简体   繁体   English

如何将用户控件对象传递给另一个用户控件的内容?

[英]How to pass the user control object to content of another user control?

I'm making a snake game in WPF C# and I created a few user controls for different "views", it means I have the MainWindow which inherits from the Window class and several user controls with their xaml files. 我正在WPF C#中制作蛇游戏,并为不同的“视图”创建了一些用户控件,这意味着我有从Window类继承的MainWindow,以及一些带有xaml文件的用户控件。 One user control represents the main menu, another represents option view and so on. 一个用户控件代表主菜单,另一个用户控件代表选项视图,依此类推。

public partial class MainWindow: Window
{
    public static Menu menu;
    public MainWindow()
    {
        InitializeComponent();
        menu = new Menu();
        this.Content = menu;
    }
}

like above, in MainWindow I create Menu object (it's a one of the user controls - class and xaml file) and set the content of Window to content of Menu. 像上面一样,在MainWindow中,我创建Menu对象(这是用户控件之一-类和xaml文件),并将Window的内容设置为Menu的内容。 And then in menu class I do the same, for example like user click on button with text "options", he goes to options user control. 然后,在菜单类中,我也执行相同的操作,例如,用户单击带有“选项”文本的按钮,便转到选项用户控件。 I just do 我只是做

this.Content = new Options(); //"this" is now menu class

when he clicks the button with text "singleplayer", he goes to user control with singleplayer game 当他单击带有“单人”文本的按钮时,他将进入单人游戏的用户控制

this.Content = new Game();

and so on. 等等。 In this way everything works fine, I can switch between different user controls and "load" different contents to application Window, but every time I create new object and it's the problem. 这样,一切正常,我可以在不同的用户控件之间切换,并将不同的内容“加载”到应用程序窗口中,但是每次创建新对象时,这就是问题所在。 When I go to options and then back to menu, a new object of menu class is creating, I cannot remember the previous settings and etc. I would like to create this only once and then reference to this - load existing object content. 当我转到选项然后返回菜单时,正在创建菜单类的新对象,我不记得以前的设置等了。我只想创建一次,然后引用它-加载现有的对象内容。 I tried using binding but it doesn't work. 我尝试使用绑定,但是不起作用。 Ho can I do this? 我能做到吗? How can I switch between different user controls without data loss and creating new object every time? 如何在不丢失数据和每次创建新对象的情况下在不同的用户控件之间切换?

You should use singletons . 您应该使用单例

Singletons allow you to have only one instance of a class . 单例仅允许一个类的一个实例。 This way, every time you manipulate the instance, you manipulate the same one. 这样,每次您操纵实例时,您都操纵同一实例。 This allow you to keep / update the state of the same instance through your code. 这使您可以通过代码保留/更新同一实例的状态。

It's looking like this, and it's thread safe. 看起来像这样,而且是线程安全的。

public sealed class YourClass
{
    private static readonly YourClass instance = new YourClass();

    /* Explicit static constructor to tell C# compiler
     * not to mark type as beforefieldinit */
    static YourClass()
    {

    }

    private YourClass()
    {

    }

    public static YourClass Instance
    {
        get
        {
            return instance;
        }
    }
}

EDIT : OP's mistake was to set Content of the usercontrol istelf insteand of MainWindow usercontrol. 编辑 :OP的错误是设置MainWindow用户控件的usercontrol istelf insteand的内容。 You can get the current window containing usercontrol by using this line of code inside usercontrol. 您可以使用usercontrol中的这一行代码来获取包含usercontrol的当前窗口。

Window yourParentWindow = Window.GetWindow(this);

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

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