【问题标题】:WPF UserControl instead of Window in ViewModelWPF UserControl 而不是 ViewModel 中的 Window
【发布时间】:2015-06-23 14:08:04
【问题描述】:

如何将 MenuManagement 的 xaml 从 Window 更改为 UserControl?

我想在 MainWindow xaml 中使用 MenuManagement,就像这样:

        this.mainViewModel = new MainWindow();
        this.mainViewModel.Show();
        this.mainViewModel.LayoutMain.Navigate(new MenuManagement());

但我收到错误: “附加信息:‘***.Views.MenuManagement’根元素对导航无效。”

我的 MenuViewModel:

    class MenuViewModel : ViewModelBase<IMainView>
{
    public MenuViewModel()
        :base(new MenuManagement())
    { //...
}}

菜单管理的xaml

<Window x:Class="***.Views.MenuManagement"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    Title="MenuManagement" Height="720" Width="1280">
<Grid>
   ...
</Grid>

菜单管理

    public partial class MenuManagement : Window, IMainView
{
    public MenuManagement()
    {
        InitializeComponent();
    } }

编辑: 接口

    public interface IView
{
    object DataContext { get; set; }
    void Close();
}

    public interface IMainView : IView
{
    void Show();
}

}

【问题讨论】:

    标签: wpf user-controls window


    【解决方案1】:

    改变这个:

    <Window x:Class="***.Views.MenuManagement"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
        Title="MenuManagement" Height="720" Width="1280">
    <Grid>
       ...
    </Grid>
    

    到:

    <UserControl x:Class="***.Views.MenuManagement"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
        Title="MenuManagement" Height="720" Width="1280">
        <Grid>
           ...
        </Grid>
    </UserControl>
    

    还有这个:

    public partial class MenuManagement : Window, IMainView
    {
        public MenuManagement()
        {
            InitializeComponent();
        } 
    }
    

    到:

    public partial class MenuManagement : UserControl, IMainView
    {
        public MenuManagement()
        {
            InitializeComponent();
        } 
    }
    

    【讨论】:

    • 我收到一个错误:错误 1 ​​'.Views.MenuManagement' 没有实现接口成员 '.Views.ViewInterfaces.IView.Close( )' 和 IMainView.Show()
    • @BeataK 查看更新。我删除了接口。如果你想让它实现接口,只需添加接口需要的那些方法即可。
    • 如果我要删除接口,我会收到另一个错误“错误 1 ​​最佳重载方法匹配 'SafaPOS.WPF.ViewModels.ViewModelBase.ViewModelBase(** *.Views.ViewInterfaces.IMainView)' 有一些无效参数“。所以我需要添加接口需要的那些方法。我希望它会有所帮助
    • @BeataK,是的,只需将接口添加回 UserControl 并实现它要求的方法。
    • 我仍然无法达到我在 MenuViewModel 中执行的所有操作的结果。设计现在还可以,但我需要使用 MenuViewModel 中的所有方法...
    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多