Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据模板和MVVM框架_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# 数据模板和MVVM框架

C# 数据模板和MVVM框架,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,当我继续尝试创建一个简单的示例时,我在使用MVVM概念处理数据模板时遇到了很多问题 为了熟悉数据模板,我们的想法如下。只需创建一个DataTemplate,其中包含绑定到我的视图模型的内容(在本例中为显示名称的标签和显示年龄的按钮) 在提问之前,这里是代码 App.xaml.cs namespace tutorial { /// <summary> /// Interaction logic for App.xaml /// </summary>

当我继续尝试创建一个简单的示例时,我在使用MVVM概念处理数据模板时遇到了很多问题

为了熟悉数据模板,我们的想法如下。只需创建一个DataTemplate,其中包含绑定到我的视图模型的内容(在本例中为显示名称的标签和显示年龄的按钮)

在提问之前,这里是代码

App.xaml.cs

namespace tutorial
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private MainWindow mw;
        private ViewModel vm;

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            vm = new ViewModel();
            mw = new MainWindow();
            mw.DataContext = ?
            mw.Activate();
            mw.Show();
        }
    }
}
namespace tutorial
{
    class ViewModel
    {
        private String name;

        private int age;

        public ViewModel()
        {
            Debug.WriteLine("Hello World!");
            name = "Hello World";
        }

        public int Age
        {
            get {return age;}
            set {age = value;}
        }

        public String Name
        {
            get {return name;}
            set {name = value;}
        }
    }
}
按钮.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:tutorial">
    <DataTemplate DataType="{x:Type local:ViewModel}">
        <StackPanel>
            <Label Content="{Binding ??}" />
            <Button Content="{Binding ???}" />
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>
<Window x:Class="tutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ContentControl Content="{Binding}" />
</Window>

main window.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:tutorial">
    <DataTemplate DataType="{x:Type local:ViewModel}">
        <StackPanel>
            <Label Content="{Binding ??}" />
            <Button Content="{Binding ???}" />
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>
<Window x:Class="tutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ContentControl Content="{Binding}" />
</Window>

因此,我有以下问题:

下面的代码行显示名称空间中不存在名称ViewModel,显然它是正确的?还是我遗漏了什么?解决方案资源管理器的图像如下所示:

第二个问题更重要,它还表明我现在知道如何将视图绑定到viewmodel。App.xaml.csButton.xaml中的问号,以及MainWindow.xaml中可能出现的一些错误,因为我不知道主窗口如何知道我希望显示哪些内容

谢谢你的帮助

以下是对整个项目的一个总结,作为对前面评论的回应:


您需要将Viewmodel设置为公共类,它确实存在于名称空间中,但其他类无法看到它

namespace tutorial
{
    public class ViewModel
    {
    }
}
数据模板所需的绑定

<DataTemplate DataType="{x:Type local:ViewModel}">
    <StackPanel>
        <Label Content="{Binding Age}" />
        <Button Content="{Binding Name}" />
    </StackPanel>
</DataTemplate>
添加资源字典

<Application.Resources>
<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Button.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

您也可以这样做

在App.xaml.cs类中

 public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        var vm = new ViewModel();
        var window = new MainWindow();
        window.DataContext = vm;
        window.Show();
    }
}
在App.xaml中,确保从App.xaml中删除StartupUri命名空间,否则将出现两个主窗口

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Button.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>


其余ViewModel.cs和Mainwindow.xaml的代码保持不变…

很抱歉,向其中添加public似乎无法解决此问题。您的意思是什么?它解决了异常吗?我将添加一些代码以向您展示另一个答案。它没有解决第一个问题(请参阅新附加的图片)。您是否尝试运行该应用程序?有时,xaml会显示在使用您提供的代码运行应用程序后实际上并不存在的错误。没有编译错误,它可以运行,但不起作用。我仍然没有看到任何按钮或标签。请将您的ViewModel设置为public,它应该可以工作。很抱歉,它没有解决第一个问题。它似乎仍然找不到ViewModel。资源字典资源在哪里,我想它的app.xaml..是吗??