显示UserControl崩溃VS XAML UI设计器

本文关键字:UI XAML UserControl 崩溃 VS 显示 | 更新日期: 2023-09-27 18:16:07

我有一个ContentControlContent属性绑定到我的ViewModel中的UserControl,就像我在这个问题中读到的:WPF:我如何动态加载用户控件?然而,当试图测试这一点,我的应用程序甚至没有运行,我得到一个消息,Visual studio XAML UI设计器意外崩溃,让我没有任何线索如何以及为什么会发生这种情况。

此外,在我的代码中,您可能会注意到我使用UserControl而不是FrameworkElement,如上面提到的问题所建议的,我尝试了两者,但它们都给了我相同的错误。

编辑:经过一些测试,我发现问题出在这行代码中,尽管我不明白为什么
    private UserControl _currentControl = new TableView();

EDIT2:上面的代码应该没有问题,因为TableView是一个UserControl,当我构建应用程序时,我没有得到任何错误

一如既往,任何帮助都将非常感激!

MainWindow.xaml

<Fluent:RibbonWindow x:Class="DatabaseExplorer.MainWindow"
        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:Fluent="clr-namespace:Fluent;assembly=Fluent"
        xmlns:View="clr-namespace:DatabaseExplorer.Views"
        xmlns:ignore="http://www.ignore.com"
        mc:Ignorable="d ignore"
        Height="300"
        Width="300"
        Title="Application"
        DataContext="{Binding Main, Source={StaticResource Locator}}">
    <Fluent:RibbonWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Fluent:RibbonWindow.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Fluent:Ribbon>
            ...
        </Fluent:Ribbon>
        <ContentControl Grid.Row="1"  Content="{Binding CurrentControl}" />
    </Grid>
</Fluent:RibbonWindow>

MainViewModel.cs

...
    /// <summary>
    /// The <see cref="CurrentControl" /> property's name.
    /// </summary>
    public const string CurrentControlPropertyName = "CurrentControl";
    private UserControl _currentControl = new TableView();
    /// <summary>
    /// Sets and gets the CurrentControl property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public UserControl CurrentControl
    {
        get
        {
            return _currentControl;
        }
        set
        {
            if (_currentControl == value)
            {
                return;
            }
            RaisePropertyChanging(CurrentControlPropertyName);
            _currentControl = value;
            RaisePropertyChanged(CurrentControlPropertyName);
        }
    }
...

显示UserControl崩溃VS XAML UI设计器

请不要调用你的类MainViewModel和把代码在它与UserControl!这不是MVVM的方式:)

如果你想在MVVM中处理一些CurrentWorkspace,那么你应该使用Viewmodels和datatemplate。因此,要将用户控件设置为CurrentWorkspace——只需设置一个视图模型。

所有的ContentControl Content属性需要的是一个Datatemplate来知道如何渲染你的视图模型。

MainViewModel.cs

 public object CurrentWorkspace {get;set;}
 ...
 this.CurrentWorkspace = this._myViewmodelInstanceWithSomeCoolThings;

xaml

<ContentControl Grid.Row="1"  Content="{Binding CurrentWorkspace }" />

内容控件保持不变,但需要一个数据模板来呈现视图模型

xaml - resources

 <Fluent:RibbonWindow.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <DataTemplate DataType="{x:Type local:ViewModelWithSomeCoolthingsClass}">
          <local:MyViewForViewModelWithSomeCoolthingsClass />
        </DataTemplate>
    </ResourceDictionary>      
 </Fluent:RibbonWindow.Resources>