XAML 在Avalonia中,为什么同一个主窗口有两个不同的ViewModel示例?

yquaqz18  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(103)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
昨天就关门了。
Improve this question
在MainWindow的代码隐藏的构造函数中,定义一个MainWindowViewModel并将其分配给DataContext。

public partial class MainWindow : Window
{
    private readonly MainWindowViewModel viewModel;
    public MainWindow()
    {
        InitializeComponent();
        viewModel = new MainWindowViewModel();  
        DataContext = viewModel;
    }

    // other code ..
}

字符串
然后在MainWindow中有两个按钮,一个按钮的Click绑定到代码隐藏中的OnClick函数,另一个按钮的Command绑定到MainWindowViewModel中的ClickCommand。

<Window x:Class="Test.Views.MainWindow"
    xmlns="https://github.com/avaloniaui"
    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:vlc="using:LibVLCSharp.Avalonia"
    xmlns:vm="using:Test.ViewModels"
    Title="Test"
    d:DesignHeight="450"
    d:DesignWidth="800"
    x:DataType="vm:MainWindowViewModel"
    Icon="/Assets/avalonia-logo.ico"
    mc:Ignorable="d">

    <Grid RowDefinitions="1*,1*">
         <StackPanel Grid.Row="1"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Spacing="10">
             <Button Width="150"
                Height="50"
                VerticalContentAlignment="Center"
                Click="Button_Click"
                Content="ClickButton" />
             <Button Width="150"
                Height="50"
                VerticalContentAlignment="Center"
                Command="{Binding ClickCommandCommand}"
                Content="CommandButton" />
        </StackPanel>
    </Grid>
</Window>


这两个函数都输出ViewModel的哈希代码

// In MainWindow.axmal.cs
public partial class MainWindow : Window
{
  // constructor..

    private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        Debug.WriteLine("this is in Button_Click:{0}",viewModel.GetHashCode());
    }
}

// In MainWindowViewModel.cs
public partial class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel(){}

    [RelayCommand]
    private void ClickCommand()
    {
        Debug.WriteLine("this is in Button_Command:{0}", this.GetHashCode());
    }
}


当单击这两个按钮时,输出的HashCode是不同的,这证明它们是两个不同的ViewModel示例。Output Result
那么,为什么同一个MainWindow会有两个不同的ViewModel示例呢?
我以前遇到过一个bug,通过Click更新ViewModel中的ObsverableCollection后,UI不会更新。只有在使用Command更新ObsverableCollection后,UI才会正确显示。
谢谢你的帮助!

xcitsw88

xcitsw881#

我想我已经找到了答案。如果你使用的是Avalonia的MVVM项目模板,你不需要像我一样在MainWindow的构造函数中重新创建一个MainWindowViewModel并将其分配给DataContext。这些步骤已经在App Class中完成。

public partial class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            // Line below is needed to remove Avalonia data validation.
            // Without this line you will get duplicate validations from both Avalonia and CT
            BindingPlugins.DataValidators.RemoveAt(0);
            desktop.MainWindow = new MainWindow
            {
                DataContext = new MainWindowViewModel(),
            };
        }

        base.OnFrameworkInitializationCompleted();
    }
}

字符串

相关问题