我正在使用Microsoft的Community Toolkit创建一个多层WPF应用程序。我的问题是,我想使用工具包中的Source Generators,但这需要ViewModel位于分部类中。因此,例如,下面的View和ViewModel如图所示工作。当我的ViewModel需要成为分部类而不是公共类时,我如何做同样的事情?
检视:
<Window x:Class="PlantWorks.FirstPart.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:v="clr-namespace:PlantWorks.FirstPart"
xmlns:vm="clr-namespace:PlantWorks.Biz.FirstPart;assembly=PlantWorks.Biz"
mc:Ignorable="d"
Title="MainView" Height="450" Width="800">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<Grid>
<TextBlock Text="{Binding SampleText}" FontSize="24" />
</Grid>
</Window>
字符串
视图模型:
//[ObservableObject]
public class MainViewModel : ObservableObject
{
string sampleText = "Hello World";
//[ObservableProperty] private string sampleText = "Hello World!!!";
public string SampleText
{
get => sampleText;
set
{
if (sampleText == value)
{
return;
}
sampleText = value;
OnPropertyChanged();
}
}
}
型
1条答案
按热度按时间ebdffaop1#
您的ViewModel可以是
public partial class MainViewModel : ObservableObject
:字符串
源代码生成器生成类的其他部分。
您不需要
[ObservableObject]
属性。您仍然可以在XAML中引用已有的ViewModel。