简体   繁体   English

将单例属性绑定到XAML标记

[英]Binding singleton property to XAML markup

First of all, let me apologize for the super-noob question about WPF and binding. 首先,让我为有关WPF和绑定的超级菜鸟问题表示歉意。 I have started, a few days ago, to get interested in WPF and its implementation with XAML markup and C# code-behind in Visual Studio Express 2013, and I'm trying to bind the contents of a button to a property that is part of a singleton class. 几天前,我开始对WPF及其在Visual Studio Express 2013中使用XAML标记和C#代码实现的实现感兴趣,我正在尝试将按钮的内容绑定到属性的一部分单身人士班。 And I can't, apparently. 我显然不能。

What I want to do is this: I have a button saying "Start" and I want, on click, to have the button contents change to a timer + stop, something like "00:00:02 -- Stop" and, on click again, to have it change back to "Start". 我想要做的是:我有一个按钮,上面写着“开始”,我希望在单击时将按钮内容更改为计时器+停止,例如“ 00:00:02-Stop”,然后在再次单击,将其更改回“开始”。

I have this class that I have designed as a singleton to prevent it from being instantiated more than once, which contains an instance stopwatch of System.Diagnostics.Stopwatch , and a string property which I change either to "Start" or stopwatch.Elapsed.TotalSeconds.ToString () + "Stop" back and forth. 我将此类设计为单例,以防止它被多次实例化,其中包含System.Diagnostics.Stopwatch的实例stopwatch和一个字符串属性,我将其更改为"Start"stopwatch.Elapsed.TotalSeconds.ToString () + "Stop"来回。

The problem is that when I try to refer to my singleton class in the XAML markup, like so (to be honest, I don't really know what I'm doing, I'm just inspiring myself from diverse examples I've found over the web): 问题是,当我尝试在XAML标记中引用我的单例类时(老实说,我真的不知道自己在做什么,我只是从我发现的各种示例中启发自己通过网络):

<Window
    ...
    xmlns:local="clr-namespace:myNameSpace">

    <Grid>
        <local:mySingleton x:Key="myStringProperty" />
        ...
    </Grid>
</Window>

I get a slew of compiler errors (6 total) of which some say: "The type mySingleton does not include any accessible constructors." 我遇到了许多编译器错误(共6个),其中有些人说:“类型mySingleton不包含任何可访问的构造函数。” and "The "Key" attribute can only be used on an element that is contained in "IDictionary"." 和““ Key”属性只能用于“ IDictionary”中包含的元素。“

I'm clueless as to what to do, knowing that I don't want to make the constructor public (and therefore get rid of the singleton thing). 我不知道该怎么做,因为我不想公开构造函数(因此摆脱了单例的事情)。

Any pointers towards the right direction ? 任何指向正确方向的指针吗?

Thank you guys for your help. 谢谢你们的帮助。

At first you have problem in displaying your string property. 起初,您在显示字符串属性时遇到问题。 You have to use text box and then use property for text binding: 您必须使用文本框,然后使用属性进行文本绑定:

<TextBox Text="{Binding Path=myStringProperty}"/>

Also you have to set your class (view model) to the Window.DataContext and your property have to call OnPropertyChanged event (see: WPF: simple TextBox data binding ). 另外,您还必须将类(视图模型)设置为Window.DataContext,并且属性必须调用OnPropertyChanged事件(请参见: WPF:简单的TextBox数据绑定 )。

Now about singleton. 现在关于单例。 The singleton shouldn't be used for this. 单例不应该用于此目的。 You should have window (view) and that window have to work with some class (view model, you have one instance per one window) and if you want to run more windows together and you always want same result, then inside that view model you should have some static object (it can be that timer, or some class which will handle requests about get result from timer which will inside that class what can be singleton). 您应该有一个窗口(视图),并且该窗口必须与某个类一起工作(视图模型,每个窗口有一个实例),如果您想一起运行更多窗口,并且始终希望得到相同的结果,那么在该视图模型内应该有一些静态对象(可以是计时器,也可以是一些类,它将处理来自计时器的获取结果的请求,该类内部可以是单例)。 So it could looks like Window (view) -> view model class -> static timer / class which will works with timer inside itself. 因此它看起来像Window(视图)->视图模型类->静态计时器/类,它将与内部计时器一起使用。

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

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