小编Ama*_*duh的帖子

将静态对象添加到资源字典中

我有一个在多个视图中引用的类,但我希望它们之间只共享一个类的实例.我已经实现了我的课程:

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法可以将Singleton.Instance作为资源添加到我的资源字典中?我想写点类似的东西

<Window.Resources>
    <my:Singleton.Instance x:Key="MySingleton"/>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

而不是{x:static my:Singleton.Instance}每次我需要引用它时都要写.

wpf mvvm resourcedictionary

13
推荐指数
2
解决办法
1万
查看次数

强制protobuf-net忽略IEnumerable/ICollection接口

如何让protobuf-net的v2忽略我的类实现ICollection,IEnumerable等的事实?

对于这种特殊情况,我只希望标记为[ProtoMember]的字段被序列化.


我目前正在使用protobuf-net v1转换为使用v2.我有一个特殊的结构,由于更改,现在正在序列化错误.它看起来像下面这样:

[ProtoContract]
public class FileTree : ICollection<FilePath>, IEnumerable<FilePath>, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged {

    private FileTreeNode _Root;

    [ProtoMember (1)]
    public FileTreeNode Root {
        get { return _Root; }
        set { _Root = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

编写FileTree类是为了将文件路径(如"C:\ happy.txt""C:\ history.txt")折叠成更像的东西

"C:\h"
???? "appy.txt"
???? "istory.txt"
Run Code Online (Sandbox Code Playgroud)

该结构消除了路径串中的冗余.所以,我真的不希望FileTree类通过IEnumerable函数被序列化,因为它只是存储为"C:\ happy.txt","C:\ history.txt"等.现在,在序列化中对于FileTree对象,每个路径都将完全打印出来.


编辑:我要提到的最后一件事 - 我在FileTree中有一个On_Deserialization函数,它用[ProtoAfterDeserialization]标记.我在函数中放了一个断点,但它没有被击中.这与此类序列化的方式有关吗?

c# wpf serialization .net-4.0 protobuf-net

6
推荐指数
1
解决办法
1230
查看次数

IMul​​tiValueConverter值没问题,但CommandParameter为null

我试图通过IMultiValueConverter将几个值传递给命令(作为命令参数).这些值在通过转换器时是正确的,但是一旦调用了Can_Execute()和Execute()命令,我就会获得一个空对象数组.有任何想法吗?

XAML:

    <Button Content="+" HorizontalAlignment="Right" VerticalAlignment="Top" Width="23" Height="23" Margin="0,0,0,0">
        <Button.CommandParameter>
            <MultiBinding Converter="{StaticResource Converter_MultipleValues}">
                <Binding/>
            </MultiBinding>
        </Button.CommandParameter>
        <Button.Command>
            <Binding Path="Command_Add_Files" Source="{StaticResource Vm_FileList}"/>
        </Button.Command>
    </Button>
Run Code Online (Sandbox Code Playgroud)

IMul​​tiValueConverter类:

class cvt_multivalue : IMultiValueConverter {
    public object Convert (object[] Values, Type Target_Type, object Parameter, CultureInfo culture) {
        if (Target_Type != typeof (object)) throw new NotSupportedException ();
        return Values;
        }

    public object [] ConvertBack (object Value, Type [] Target_Type, object Parameter, CultureInfo culture) {
        throw new NotSupportedException ();
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我没有使用MultiBinding和转换器时,代码工作得很好,但我需要MultiBinding,所以我可以将一些额外的信息传递给命令.

wpf multibinding commandparameter

3
推荐指数
1
解决办法
2069
查看次数