在wp8 app中使用单例填充页面上的数据 - 生成错误

时间:2013-02-04 14:28:36

标签: c# telerik windows-phone-8

我正在使用RadDataForm,这是来自telerik for wp8的UI控件的一部分。我遇到的问题与控制无关,而是与我做过或不理解的愚蠢有关。

我有一个名为ProfileFormData的类,用于填充DataForm中的DatFields。它从名为Profile的类获取其属性,该类是单例。

好的,首先是代码。

Public class Profile
{
   Public sting Name {get;set;}
   Public int Gender {get;set;}
}

public sealed class Globals
{
    /// <summary>
    /// Current profile
    /// </summary>
    public Profile profile        

    private static readonly SingleInstance<Globals> lazy =
        new SingleInstance<Globals>(() => new Globals());

    public static Globals Instance { get { return lazy.Instance; } }
}

  internal sealed class SingleInstance<T> where T : class
  {
    private readonly Object m_lockObj = new object();
    private readonly Func<T> m_delegate;
    private Boolean m_isDelegateInvoked;

    private T m_value;

    /// <summary>
    /// Initializes a new instance of the 
    /// <see cref="SingleInstance&lt;T&gt;"/> class.
    /// </summary>
    public SingleInstance() : this(() => default(T)) { }

    /// <summary>
    /// Initializes a new instance of the 
    /// <see cref="SingleInstance&lt;T&gt;"/> class.
    /// </summary>
    /// <param name="delegate">The @delegate</param>
    public SingleInstance(Func<T> @delegate)
    {
        m_delegate = @delegate;
    }

    /// <summary>
    /// gets the instance
    /// </summary>
    /// <value>The instance</value>
    public T Instance
    {
        get
        {
            if (!m_isDelegateInvoked)
            {
                T temp = m_delegate();
                Interlocked.CompareExchange<T>(ref m_value, temp, null);

                Boolean lockTaken = false;

                try
                {
                    // WP7 does not support the overload with the
                    // boolean indicating if the Lock was taken
                    Monitor.Enter(m_lockObj); lockTaken = true;
                    m_isDelegateInvoked = true;
                }
                finally
                {
                    if (lockTaken) { Monitor.Exit(m_lockObj); }
                }
            }

            return m_value;
        }
    }
}

ProfileFormData

 public class ProfileFormData
 {
    public string Name
    {
        get { return Globals.Instance.profile.UserName; }
        set { Globals.Instance.profile.UserName = value; }
    }

    [GenericListEditor(typeof(GenderInfoProvider))]
    public Gender Gender
    {
        get { return (Gender)Globals.Instance.profile.Gender; }
        set { Globals.Instance.profile.Gender = (int)value; }
    }
}  

现在是xaml,其中错误显示在blend和vs designer

<Grid Grid.Row="1" Margin="0,6,0,6">
        <ScrollViewer>
            <telerikInput:RadDataForm Grid.Row="1" Margin="12,0,12,12" x:Name="DataForm">
                <telerikInput:RadDataForm.CurrentItem>
                    <models:ProfileFormData/>
                </telerikInput:RadDataForm.CurrentItem>
                <Grid>
                    <telerikInput:DataField Header="Name" TargetProperty="Name"/>
                    <telerikInput:DataField Header="Gender" TargetProperty="Gender"/>
                </Grid>
            </telerikInput:RadDataForm>
        </ScrollViewer>
    </Grid>

上面的所有xaml下面都有一条红线,我收到了错误

  

对象未设置为对象的实例

部署和运行应用程序可以正常工作。

哦,我在App.xaml.c中创建了这样的配置文件

    /// <summary>
    /// Constructor for the Application object.
    /// </summary>
    public App()
    {
        // Global handler for uncaught exceptions. 
        UnhandledException += Application_UnhandledException;

        // Standard Silverlight initialization
        InitializeComponent();

        // Phone-specific initialization
        InitializePhoneApplication();

        Globals.Instance.profile = new Profile();
    }

0 个答案:

没有答案