小编Wal*_*eed的帖子

自定义控件上的TemplateBinding到DependencyProperty不起作用

目前,我正在开发一个简单的自定义按钮,该按钮使用用户提供的图像作为按下和正常状态的背景.我有很多按钮,所以我决定编写一个自定义按钮,并为按下和正常状态的图片实现两个属性.

这是我正在使用的代码

public partial class ThemeableButton : Button
{
    public ThemeableButton()
    {
        InitializeComponent();
    }


    public static readonly DependencyProperty PressedContentBackgroundSourceProperty = DependencyProperty.Register(
                    "PressedContentBackgroundSource", typeof(ImageSource), typeof(ThemeableButton), null);
    public ImageSource PressedContentBackgroundSource
    {
        get { return (ImageSource)GetValue(PressedContentBackgroundSourceProperty); }
        set
        {
            (value as BitmapImage).CreateOptions = BitmapCreateOptions.BackgroundCreation; 
            SetValue(PressedContentBackgroundSourceProperty, value);
        }
    }


    public static readonly DependencyProperty NormalContentBackgroundSourceProperty =
        DependencyProperty.Register("NormalContentBackgroundSource", typeof(ImageSource), typeof(ThemeableButton), null);

    public ImageSource NormalContentBackgroundSource
    {
        get { return (ImageSource)GetValue(NormalContentBackgroundSourceProperty); }
        set
        {
            (value as BitmapImage).CreateOptions = BitmapCreateOptions.BackgroundCreation;
            SetValue(NormalContentBackgroundSourceProperty, value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我为这个按钮编写了如下样式

        <Style x:Key="ThemeableButtonTemplate" TargetType="MJbox_UIComponents_Controls:ThemeableButton"> …
Run Code Online (Sandbox Code Playgroud)

c# silverlight xaml windows-phone-7

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

将字节数组封送到C#结构

我正在研究用于读取FAT32引导扇区和BPB的C#项目,问题是我正在使用编组机制将字节数组转换为自定义FAT32数据结构.我收到一条消息错误说:无法从程序集"FAT32Management,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null"加载类型"FAT32Management.Fat32BootSector",因为它包含偏移量为3的对象字段未正确对齐或重叠由非对象字段.

我无法解决问题

这是代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace FAT32Management
{
    [StructLayout(LayoutKind.Explicit)]//, CharSet = CharSet.Ansi, Size = 96, Pack = 1)]
    public struct Fat32BootSector
    {
        #region Common Region With all FAT systems
        /// <summary>
        /// First 3 Bytes of the Jump insctructions.
        /// Offset 0x00  
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        [FieldOffset(0x00)]
        public byte[] JumpBootInstructions;

        /// <summary>
        /// 8 Bytes of the OemName
        /// Offset 0x03
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, …
Run Code Online (Sandbox Code Playgroud)

c# unmanaged marshalling

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

这个Singleton实现有什么问题?

这个想法是在程序结束时删除C++中的Singleton.我们在课堂上学到了这种实现方法:

class Singleton
{

private:
    static Singleton* the_singleton;

protected:
    Singleton()
    {
        static Keeper keeper(this);
        /*CONSTRUCTION CODE*/
    }
    virtual ~Singleton()
    {
        /*DESTRUCTION CODE*/
    }

public:
    class Keeper
    {

    private:
        Singleton* m_logger;

    public:
        Keeper(Singleton* logger):m_logger(logger){}

        ~Keeper()
        {
            delete m_logger;
        }
    };
    friend class Singleton::Keeper;

    static Singleton* GetInstance();
    {
        if (!the_singleton)
            the_singleton = new Singleton();
        return the_singleton;
    }
};

Singleton* Singleton::the_singleton = NULL;
Run Code Online (Sandbox Code Playgroud)

我们的想法是,在第一次创建Singleton时,将在Singleton的C'tor中创建一个静态Keeper对象,一旦程序结束,Keeper将被销毁,反过来将破坏它指向的Singleton实例至.

现在,这个方法对我来说似乎相当麻烦,所以我建议抛弃守护者类并使Singleton的实例成为getInstance方法的静态对象:

<!-- language: c++ -->

class Singleton
{

protected:
    Singleton()
    {
        /*CONSTRUCTION CODE*/
    }

    ~Singleton()
    {
        /*DESTRUCTION CODE*/
    } …
Run Code Online (Sandbox Code Playgroud)

c++ singleton

5
推荐指数
2
解决办法
1223
查看次数

执行DeleteOnSubmit(),SubmitChanges(),InsertOnSubmit(),SubmitChanges()不起作用(抛出实体已存在异常)!

我正在开发一个WP7芒果应用程序,它利用Linq2SQL进行数据访问.我有一个Note实体,它有一个类型为int的自动生成键.

我第一次向数据库添加新注释时,操作正常,注释保存,然后如果我从数据库中删除它,它也会从数据库中删除.第一个实体始终为Id = 0.

然后,如果我想在删除第一个音符后添加新音符,我会得到一个例外,表明该实体已经存在.我得出结论,即使我在数据上下文中调用SubmitChanges,也没有删除Id = 0的第一个实体.

此外,我在我的存储库和相同的存储库实例(出于性能原因的单例)上使用相同的数据上下文进行数据操作.为了确认这种行为,我试图连续调用,但它失败了!

this.DbContext.Notes.DeleteOnSubmit(value);
this.DbContext.SubmitChanges();
this.DbContext.Notes.InsertOnSubmit(value);
this.DbContext.SubmitChanges();
Run Code Online (Sandbox Code Playgroud)

它表示它无法添加已存在的实体.对此行为有何解释?提前致谢.

注意:当我使用数据上下文的两个不同实例时,此行为将消失.

c# silverlight linq-to-sql windows-phone-7

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