重用XAML块的最佳方法是什么?

Edw*_*uay 11 c# wpf xaml

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

PageManageCustomers.xaml.cs:

public partial class PageManageCustomers : BasePage
{
 ...
}
Run Code Online (Sandbox Code Playgroud)

继承自:

PageBase.cs:

public class BasePage : UserControl, INotifyPropertyChanged
{
 ...
}
Run Code Online (Sandbox Code Playgroud)

由于PageBase.cs没有伴随XAML文件,我必须把它指的是在XAML 每个其继承它用户控制的,例如,反复进行以下的块的每个每一个继承控制的XAML文件PageBase:

<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>
Run Code Online (Sandbox Code Playgroud)

我试图将此块放入资源文件但无法正确获取语法,它说:

'ResourceDictionary'根元素需要ax:Class属性来支持XAML文件中的事件处理程序.删除MouseDown事件的事件处理程序,或将x:Class属性添加到根元素.

或许我可以用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>
Run Code Online (Sandbox Code Playgroud)

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

Page1.xaml.cs:

namespace TestXamlPage8283
{
    public partial class Page1 : BasePage
    {
        public Page1()
        {
            InitializeComponent();
            PageTitle = "Page One";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Page2.xaml.cs:

namespace TestXamlPage8283
{
    public partial class Page2 : BasePage
    {
        public Page2()
        {
            InitializeComponent();
            PageTitle = "Page Two";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

怎么做这个

<TextBlock Text="{Binding PageTitle}"
           FontSize="14"
           FontWeight="Bold"/>
Run Code Online (Sandbox Code Playgroud)

Page1.xaml和Page2.xaml中将它放在一个地方,以便我可以从Page1.xaml和Page2.xaml 引用它?(所以当我想将FontSize = 14更改为FontSize = 16时,我只需要在一个地方更改它)

Mar*_*art 6

使用资源字典 - MyDictionary.xaml向项目添加文件,将其Build Action设置为"Page":

<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>
Run Code Online (Sandbox Code Playgroud)

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

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MyDictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    ... some other local resources ...
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

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

  • 没错,您有`MouseDown`事件,这些事件在编译时无法绑定,因为字典中没有任何代码。您应该在运行时添加事件处理程序,或使用以下命令:http://msdn.microsoft.com/zh-cn/library/ms752308.aspx。 (2认同)