简体   繁体   English

从WPF用户控制更改为窗口?

[英]Changing from WPF User Control to Window?

I've been working on a commandline application, and have recently decided to add a wpf window to the application. 我一直在研究命令行应用程序,最近决定在应用程序中添加一个wpf窗口。 I added this as a UserControl, however I noticed I can't call this class using ShowDialog() from my main code; 我将其添加为UserControl,但是我注意到我无法使用主代码中的ShowDialog()来调用此类;

I've tried changing the Base class from a UserControl to Window, however an error occurs; 我已经尝试将Base类从UserControl更改为Window,但是会发生错误;

public partial class UserControl1 : Window
    {
        public UserControl1()
        {
            InitializeComponent();
        }

Error 1 Partial declarations of 'ExcelExample.UserControl1' must not specify different base classesExcelExample 错误1“ExcelExample.UserControl1”的部分声明不得指定不同的基类classesExcelExample

I've added all the references found in my other WPF application to no avail. 我添加了在我的其他WPF应用程序中找到的所有引用无济于事。 Help! 救命!

In order to change the base class it is not sufficient to change it in code only. 为了更改基类,仅在代码中更改它是不够的。 You must also change the root tag and any nested elements in accompanying XAML file. 您还必须更改随附XAML文件中的根标记和任何嵌套元素。 For example, you have something like: 例如,你有类似的东西:

<UserControl x:Class="Your.Namespace.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <UserControl.Resources>
     </UserControl.Resources>
</UserControl>

You must change it to something like: 您必须将其更改为:

<Window x:Class="Your.Namespace.UserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <Window.Resources>
     </Window.Resources>
</Window>

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

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