简体   繁体   English

如何使 XAML 视图(页面)从使用 UserControl 作为泛型类型的类继承

[英]How to make a XAML view (a page) inherit from a class, that uses UserControl as generic type

I have an issue, and I don't know if I can do that, or if there is another way to do that.我有一个问题,我不知道我是否可以这样做,或者是否有其他方法可以做到这一点。 I have an abstract class called "BasePage", it is my .NET Standard library, so I can't access to System.Windows namespace.我有一个名为“BasePage”的抽象类,它是我的 .NET Standard 库,因此我无法访问 System.Windows 命名空间。 Its declaration is:它的声明是:

public abstract class BasePage<VR> where VR : new()

EDIT: The class above has a public property to access to VR编辑:上面的类有一个公共属性来访问 VR

So now, for each platform (WPF, Xamarin, etc) I have to create its own implementation of the class begin VR the content control of each platform (UserControl for WPF, for example).所以现在,对于每个平台(WPF、Xamarin 等),我必须创建自己的类 begin VR实现每个平台的内容控制(例如 WPF 的 UserControl)。 I already did it and this is:我已经做到了,这是:

public class WindowsBasePage : BasePage<UserControl>

Now, every time I want to create a page to add content to a window, I have to create something like this:现在,每次我想创建一个页面来向窗口添加内容时,我都必须创建如下内容:

 public partial class UserPassPage : WindowsBasePage
 {
     public UserPassPage()
     {
         InitializeComponent();
     }
 }

The problem becomes when I have to set it in the XAML file (the View).当我必须在 XAML 文件(视图)中设置它时,问题就来了。 I can create it and it builds:我可以创建它并构建:

<local:WindowsBasePage x:Class="Bitture.AppManager.Manager.UserPassPage"
    [...]
    xmlns:local="clr-namespace:Bitture.AppManager.Manager"
    mc:Ignorable="d" >
</local:WindowsBasePage>

but I can't add components (like buttons, text, grids, stackpanels, etc).但我无法添加组件(如按钮、文本、网格、堆栈面板等)。 I want to know If I can do it with my current code or there is something I have to change.我想知道我是否可以用我当前的代码做到这一点,或者我必须改变一些东西。 Because I have to access to the generic type WindowsBasePage that inherits from因为我必须访问继承自的通用类型WindowsBasePage

class WindowsBasePage : BasePage<UserControl>

I am not sure what you are trying to do here but you can't add UIElements to a custom class of yours that doesn't inherit from any of the common WPF base classes such as for example Panel or ContentControl , and expect it to be rendered as a UserControl or some other WPF control.我不确定您要在这里做什么,但您不能将UIElements添加到您的自定义类中,该类不继承自任何常见的 WPF 基类,例如PanelContentControl ,并期望它是呈现为UserControl或其他一些 WPF 控件。

This won't work.这行不通。 WPF knows about how to render WPF controls but it doesn't know how to render a custom BasePage<UserControl> . WPF 知道如何呈现 WPF 控件,但它不知道如何呈现自定义BasePage<UserControl>

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

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