【问题标题】:ViewModel not displayingViewModel 不显示
【发布时间】:2021-09-25 08:02:28
【问题描述】:

我正在学习如何在 C# 上使用 WPF,我遇到了一个绊脚石,我试图在单击按钮时打开一个新窗口/视图 这是第一个窗口代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PROG6221_POE_Tarisai_Gonah_18028480
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void OpenWindow(object sender, RoutedEventArgs e)
        {
            Window f1 = new Window();
            this.Visibility = Visibility.Hidden;
            f1.Show();
        }
    }
}

这是第二个窗口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PROG6221_POE_Tarisai_Gonah_18028480.Views
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void OpenWindow(object sender, RoutedEventArgs e)
        {
            MainWindow objmain = new MainWindow();
            this.Visibility = Visibility.Hidden;
            objmain.Show();
        }
    }
}

我已经设置好了,所以当点击相应的按钮时,它允许我进入第一个窗口和第二个窗口。当我运行代码时,它显示第一个视图,然后当我单击按钮转到第二个视图时,我得到一个空白屏幕

有谁知道问题出在哪里?

【问题讨论】:

    标签: c# wpf user-interface button view


    【解决方案1】:

    尝试使用 Window1 f1 = new Window1();在 MainWindow ...因为您只创建新窗口,而不是正确的窗口!使用相同的命名空间 PROG6221_POE_Tarisai_Gonah_18028480 !

    【讨论】:

    • 它给了我一个错误,它说没有找到命名空间
    • 对两个窗口使用相同的命名空间 PROG6221_POE_Tarisai_Gonah_18028480 !
    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多