简体   繁体   English

用于显示UserControl的ContentControl崩溃VS XAML UI Designer

[英]ContentControl to display UserControl crashes VS XAML UI Designer

I have a ContentControl with the Content property bound to a UserControl in my ViewModel, like I read in this question: WPF: how do I load user controls dynamically? 我有一个ContentControl ,其Content属性绑定到ViewModel中的UserControl ,就像我在这个问题中看到的: WPF:如何动态加载用户控件? However when trying to test this, my app doesn't even run, I get a message that the Visual Studion XAML UI Designer has crashed unexpectedly, leaving me without any clue how and why this is happening. 但是,当尝试对此进行测试时,我的应用程序甚至无法运行,我收到一条消息,提示Visual Studion XAML UI设计器意外崩溃,这使我不知道这是如何发生以及为什么发生的任何线索。

Also, in my code you may notice I use a UserControl instead of FrameworkElement as suggested in the above mentioned question, I tried both but they each give me the same error. 另外,在我的代码中,您可能会注意到,我使用UserControl代替了上面提到的问题中建议的FrameworkElement ,我尝试了两者,但它们都给了我相同的错误。

EDIT: After some testing, I found out the problem lies in this line of code, although I don't see why 编辑:经过一些测试,我发现问题出在这行代码,尽管我不明白为什么

    private UserControl _currentControl = new TableView();

EDIT2: There code above shouldn't be a problen though because TableView is a UserControl , and when I build the application I get no errors EDIT2:尽管上面的代码不应该存在问题,因为TableView是UserControl ,并且在我构建应用程序时没有出现错误

As always, any help will be greatly appreciated! 一如既往,任何帮助将不胜感激!

MainWindow.xaml 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 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);
        }
    }
...

pls dont call your class MainViewModel and put code in it with UserControl! 请不要调用您的类MainViewModel并使用UserControl将代码放入其中! thats not the MVVM way :) 那不是MVVM方式:)

if you wanna handle some kind of CurrentWorkspace in MVVM then you should work with Viewmodels and DataTemplates. 如果您想在MVVM中处理某种CurrentWorkspace,则应使用Viewmodels和DataTemplates。 so instead to set a usercontrol to the CurrentWorkspace - simply set a viewmodel. 因此,将用户控件设置为CurrentWorkspace只需设置一个视图模型即可。

all the ContentControl Content property needs is a Datatemplate to know how to render your viewmodel. ContentControl Content属性所需的全部是一个Datatemplate,以了解如何呈现视图模型。

MainViewModel.cs MainViewModel.cs

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

xaml a

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

content control stays the same, but needs a datatemplate to render your viewmodel 内容控件保持不变,但需要一个数据模板来呈现您的视图模型

xaml - resources xaml-资源

 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM