【问题标题】:What is the best way to reuse blocks of XAML?重用 XAML 块的最佳方法是什么?
【发布时间】:2009-11-26 12:31:32
【问题描述】:

我有很多这样的用户控件:

PageManageCustomers.xaml.cs:

public partial class PageManageCustomers : BasePage
{
 ...
}

继承自:

PageBase.cs:

public class BasePage : UserControl, INotifyPropertyChanged
{
 ...
}

由于 PageBase.cs 没有没有随附的 XAML 文件,我必须将它引用的 XAML 放在 每个 用户控件中继承它,例如以下块在继承PageBase 的每个控件的每个 XAML 文件中重复:

<DataTemplate x:Key="manageAreaCellTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Style="{DynamicResource ManageLinkStyle}"
    Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
        <TextBlock Text=" "/>
        <TextBlock Style="{DynamicResource ManageLinkStyle}"
           Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
    </StackPanel>
</DataTemplate>

我正在尝试将此块放入 资源文件,但语法不正确,它说:

'ResourceDictionary' 根元素 需要一个 x:Class 属性来 支持 XAML 中的事件处理程序 文件。删除事件处理程序 对于 MouseDown 事件,或添加一个 x:根元素的类属性。

或者也许我可以用 XamlReader` 以某种方式读取这些块?

我怎样才能把这个重复的代码块放在一个地方,这样它就不会在每个继承 BagePage 的 XAML 文件中重复?

这里是这个问题的一个可重现的例子:

Window1.xaml:

<Window x:Class="TestXamlPage8283.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel x:Name="MainContent"/>
</Window>

Window1.xaml.cs:

using System.Windows;

namespace TestXamlPage8283
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Page1 page1 = new Page1();
            MainContent.Children.Add(page1);

            Page2 page2 = new Page2();
            MainContent.Children.Add(page2);
        }
    }
}

Page1.xaml:

<local:BasePage x:Class="TestXamlPage8283.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestXamlPage8283"
    Height="40" Width="300">
    <StackPanel>
        <TextBlock Text="{Binding PageTitle}"
                   FontSize="14"
                   FontWeight="Bold"/>
        <TextBlock Text="This is XAML that is specific to page one." />
    </StackPanel>
</local:BasePage>

Page1.xaml.cs:

namespace TestXamlPage8283
{
    public partial class Page1 : BasePage
    {
        public Page1()
        {
            InitializeComponent();
            PageTitle = "Page One";
        }
    }
}

Page2.xaml:

<local:BasePage x:Class="TestXamlPage8283.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestXamlPage8283"
    Height="40" Width="300">
    <StackPanel>
        <TextBlock Text="{Binding PageTitle}"
                   FontSize="14"
                   FontWeight="Bold"/>
        <TextBlock Text="This is XAML that is specific to page two." />
    </StackPanel>
</local:BasePage>

Page2.xaml.cs:

namespace TestXamlPage8283
{
    public partial class Page2 : BasePage
    {
        public Page2()
        {
            InitializeComponent();
            PageTitle = "Page Two";
        }
    }
}

BasePage.cs:

using System.Windows.Controls;
using System.ComponentModel;

namespace TestXamlPage8283
{
    public class BasePage : UserControl, INotifyPropertyChanged
    {
        #region ViewModelProperty: PageTitle
        private string _pageTitle;
        public string PageTitle
        {
            get
            {
                return _pageTitle;
            }

            set
            {
                _pageTitle = value;
                OnPropertyChanged("PageTitle");
            }
        }
        #endregion

        public BasePage()
        {
            DataContext = this;
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

我该如何处理这个

<TextBlock Text="{Binding PageTitle}"
           FontSize="14"
           FontWeight="Bold"/>

out 的 Page1.xaml 和 Page2.xaml 并将其放在 一个 位置,以便我可以从 Page1 引用 .xaml 和 Page2.xaml?(这样当我想将 FontSize=14 更改为 FontSize=16 时,我只需在一处更改即可)

【问题讨论】:

  • 看来您要问的内容类似于 Asp.net 的母版页。我不知道这是否可能。对于您的 TextBlock 最后一个示例(更改 PageTitle 文本块的 FontSize),这不是样式的用途吗?

标签: c# wpf xaml


【解决方案1】:

使用资源字典 - 将MyDictionary.xaml 文件添加到您的项目中,将其构建操作设置为“页面”:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <DataTemplate x:Key="manageAreaCellTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Style="{DynamicResource ManageLinkStyle}"
              Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
            <TextBlock Text=" "/>
            <TextBlock Style="{DynamicResource ManageLinkStyle}"
              Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

然后在其他 XAML 文件中引用它:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MyDictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    ... some other local resources ...
</ResourceDictionary>

并将您的资源称为Template={StaticResource manageAreaCellTemplate}

【讨论】:

  • 当我这样做时它告诉我:“'ResourceDictionary' 根元素需要一个 x:Class 属性来支持 XAML 文件中的事件处理程序。删除 MouseDown 事件的事件处理程序,或添加一个 x:根元素的类属性。”当我添加 x:Class 属性时,出现如下错误:“在声明类型'TestApp.Pages.BasePageManageItems'时缺少部分修饰符;存在此类型的另一个部分声明”
  • 没错,你有 MouseDown 事件在编译时无法绑定,因为字典后面没有代码。您应该在运行时添加事件处理程序或使用命令:msdn.microsoft.com/en-us/library/ms752308.aspx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多