实例化和XAML对象重用实例
-
27-09-2019 - |
题
我想在XAML实例化对象,并重新使用这些实例。我想这应该是简单的,但我坚持,我可能失去了一些东西明显。
说我要猫添加到不同的房间(房间具有含有型猫的对象一个ObservableCollection)。在UserControl.Resources创建ObjectDataProviders:
<ObjectDataProvider x:Key="Cat1" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Tom</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="Cat2" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Garfield</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="Cat3" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Furball</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
在我的用户我想给猫添加到房间中:
<local:Room x:Name="Room1">
<local:Room.Cats>
</local:Room.Cats>
<local:Room>
<local:Room x:Name="Room2">
<local:Room.Cats>
</local:Room.Cats>
<local:Room>
什么是用于将Cat实例到的ObservableCollection Room.Cats语法?比如我想CAT1和CAT2到房间1,和CAT2和添加的Cat3向室2。上午我完全在错误的轨道?
解决方案 2
根据从Heinzi和罗伯特Rossney我的反馈提出了以下解决方案,与一个ObservableCollection的作品,我可以在XAML访问和后面的代码:
在代码我延伸的ObservableCollection所以我可以在XAML使用它(这将不再需要在2009 XAML):
public class CatObservableCollection : ObservableCollection<Cat> { }
在XAML在UserControl.Resources我实例化猫:
<local:Cat x:Key="Tom" Name="Tom"/>
<local:Cat x:Key="Garfield" Name="Garfield"/>
<local:Cat x:Key="Furball" Name="Furball"/>
在集合:
<local:CatObservableCollection x:Key="Room1Collection">
<StaticResourceExtension ResourceKey="Tom"/>
<StaticResourceExtension ResourceKey="Garfield"/>
</local:CatObservableCollection>
<local:CatObservableCollection x:Key="Room2Collection">
<StaticResourceExtension ResourceKey="Garfield"/>
<StaticResourceExtension ResourceKey="Furball"/>
</local:CatObservableCollection>
在房间现在被定义如下:
<local:Room x:Name="Room1" Cats="{StaticResource Room1Collection}"/>
<local:Room x:Name="Room2" Cats="{StaticResource Room2Collection}"/>
Room.Cats是一个ObservableCollection <猫>
其他提示
重用单个实例你要做到这一点是非常棘手的方式。这是因为你通常在XAML引用单个对象的方式是与StaticResource
标记扩展,并且可以只使用标记扩展来设置的属性值。
因此可以容易地设定类型Cat
的属性到Cat
的实例:
<Room Cat="{StaticResource Cat1}"/>
但你无法通过设置属性填充的集合。
答案,令人惊奇地,是直接在XAML实例化,而不是在ObjectDataProvider
s包裹它们的您的对象。你还在用ObjectDataProvider
,但不同的:
<Window x:Class="ObjectDataProviderDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ObjectDataProviderDemo"
xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<local:Cat x:Key="Tom" Name="Tom"/>
<local:Cat x:Key="Garfield" Name="Garfield"/>
<local:Cat x:Key="Furball" Name="Furball"/>
<Collections:ArrayList x:Key="CatList1">
<ObjectDataProvider ObjectInstance="{StaticResource Tom}" />
<ObjectDataProvider ObjectInstance="{StaticResource Garfield}" />
<ObjectDataProvider ObjectInstance="{StaticResource Furball}" />
</Collections:ArrayList>
<Collections:ArrayList x:Key="CatList2">
<ObjectDataProvider ObjectInstance="{StaticResource Tom}" />
<ObjectDataProvider ObjectInstance="{StaticResource Furball}" />
</Collections:ArrayList>
<DataTemplate x:Key="CatTemplate">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{StaticResource CatList1}"
ItemTemplate="{StaticResource CatTemplate}"/>
<ListBox ItemsSource="{StaticResource CatList2}"
ItemTemplate="{StaticResource CatTemplate}" />
</StackPanel>
</Window>
有关您的特殊需求,你干什么去了仿制药。并声明使用泛型,你必须使用 X: TypeArguments指令。 TypeArguments指令只能与根元素一起使用。所以,你必须现在去松散XAML文件。松散XAML文件可以使用System.Windows.Markup.XamlReader.Load(Stream Stream) method
被读取
Cat.cs:
using System;
namespace WpfCollection._3840371
{
public class Cat
{
public Cat() { }
public Cat(String name, String color) { Name = name; Color = color; }
public String Name { get; set; }
public String Color { get; set; }
}
}
Room.cs:
using System;
using System.Collections.ObjectModel;
namespace WpfCollection._3840371
{
public class Room<T> where T : System.Windows.Data.ObjectDataProvider
{
public Room()
{
Cats = new ObservableCollection<T>();
}
public ObservableCollection<T> Cats { get; set; }
}
}
窗口类:
namespace WpfCollection._3840371
{
/// <summary>
/// Interaction logic for Win3840371.xaml
/// </summary>
public partial class Win3840371 : Window
{
public Win3840371()
{
InitializeComponent();
Room<ObjectDataProvider> kitchenRoom;
using (FileStream fs = new FileStream(@"3840371/roomcats.txt", FileMode.Open))
{
kitchenRoom = (Room<ObjectDataProvider>)XamlReader.Load(fs);
}
foreach (ObjectDataProvider o in kitchenRoom.Cats)
Debug.WriteLine(((Cat)o.Data).Name + " : " + ((Cat)o.Data).Color);
}
}
}
所以,含有XAML代码的.txt文件将是:
<local:Room
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfCollection._3840371;assembly=WpfCollection"
x:Key="UpperRoom" x:TypeArguments="ObjectDataProvider">
<local:Room.Cats>
<ObjectDataProvider x:Key="Cat1" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Tom</System:String>
<System:String>Red</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="Cat2" ObjectType="{x:Type local:Cat}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Rubia</System:String>
<System:String>Brown</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
</local:Room.Cats>
</local:Room>