wpf 我在多层应用程序中使用Community.Toolkit.MVVM中的Source Generator时遇到问题

s2j5cfk0  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(156)

我正在使用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();
        }
    }
}

ebdffaop

ebdffaop1#

您的ViewModel可以是public partial class MainViewModel : ObservableObject

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty] private string sampleText = "Hello World!!!";
}

字符串
源代码生成器生成类的其他部分。
您不需要[ObservableObject]属性。
您仍然可以在XAML中引用已有的ViewModel。

相关问题