我正在使用 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<T>"/> class.
/// </summary>
public SingleInstance() : this(() => default(T)) { }
/// <summary>
/// Initializes a new instance of the
/// <see cref="SingleInstance<T>"/> 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; }
}
}
现在是在混合和 vs 设计器中都显示错误的 xaml
<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();
}