在WP7中将Text属性与ApplicationBarIconButton绑定

时间:2012-03-29 07:25:16

标签: windows-phone-7

我想将字符串绑定到WP7中ApplicationBarIconButton中的Text Property。 在这样做时,我收到了一个错误

设置属性'Microsoft.Phone.Shell.ApplicationBarIconButton.Text'引发异常。

堆栈跟踪:

  

在System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,   BindingFlags invokeAttr,Binder binder,Object []参数,   CultureInfo culture,StackCrawlMark& stackMark)               在System.Reflection.RuntimePropertyInfo.InternalSetValue(PropertyInfo   thisProperty,Object obj,Object value,Object [] index,   StackCrawlMark&安培; stackMark)               在System.Reflection.RuntimePropertyInfo.SetValue(Object obj,Object value,Object [] index)               在MS.Internal.XamlMemberInfo.SetValue(对象目标,对象值)               在MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType,   XamlQualifiedObject&安培; inObj,XamlPropertyToken inProperty,   XamlQualifiedObject&安培; inValue)               在MS.Internal.XcpImports.Application_LoadComponentNative(IntPtr   pContext,IntPtr pComponent,UInt32 cUriStringLength,String   uriString,UInt32 cXamlStrLength,Byte * pXamlStr,UInt32   cAssemblyStrLength,String assemblyStr)               在MS.Internal.XcpImports.Application_LoadComponent(IManagedPeerBase   componentAsDO,String resourceLocator,UnmanagedMemoryStream流,   UInt32 numBytesToRead,String assemblyString)               在System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator)               在Syncfusion.Phone.Tools.Controls.TimeSpanPickerPage.InitializeComponent()               在Syncfusion.Phone.Tools.Controls.TimeSpanPickerPage..ctor()               在System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo)   rtci,BindingFlags invokeAttr,Binder binder,Object parameters,   CultureInfo culture,Boolean isBinderDefault,Assembly caller,Boolean   verifyAccess,StackCrawlMark& stackMark)               在System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj,   BindingFlags invokeAttr,Binder binder,Object []参数,   CultureInfo culture,StackCrawlMark& stackMark)               在System.Activator.InternalCreateInstance(Type type,Boolean nonPublic,StackCrawlMark& stackMark)               在System.Activator.CreateInstance(类型类型)               在System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback)   userCallback,PageResourceContentLoaderAsyncResult result)               在System.Windows.Navigation.PageResourceContentLoader。<> c_ DisplayClass4.b _0(Object   参数)               在System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo   rtmi,Object obj,BindingFlags invokeAttr,Binder binder,Object   参数,CultureInfo文化,布尔isBinderDefault,汇编   调用者,布尔验证访问,StackCrawlMark& stackMark)               在System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,   BindingFlags invokeAttr,Binder binder,Object []参数,   CultureInfo culture,StackCrawlMark& stackMark)               在System.Reflection.MethodBase.Invoke(Object obj,Object []参数)               在System.Delegate.DynamicInvokeOne(Object [] args)               在System.MulticastDelegate.DynamicInvokeImpl(Object [] args)               在System.Delegate.DynamicInvoke(Object [] args)               在System.Windows.Threading.DispatcherOperation.Invoke()               在System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority   优先)               在System.Windows.Threading.Dispatcher.OnInvoke(对象上下文)               在System.Windows.Hosting.CallbackCookie.Invoke(Object [] args)               在System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object [] args)               在System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle,   Int32 nParamCount,ScriptParam [] pParams,ScriptParam& pResult)

解决方案: ApplicationBar是一个特殊元素,未在Silverlight中实现,不能与数据绑定一起使用。您必须直接更新C#中的Text属性。 Peter Torr在此解释:Peter Torr的博客> Why are the ApplicationBar objects not FrameworkElements?

2 个答案:

答案 0 :(得分:7)

在我看来你已经知道了答案,无论如何绑定数据ApplicationBar是不可能的,你可以在运行时创建应用程序栏,你可以从那里做一些事情。 ApplicationBarIconButton At Runtime

答案 1 :(得分:3)

虽然ApplicationBar不是可绑定对象,但还有一些工作策略可以使绑定工作。

一个是来自Cimbalino Windows Phone ToolkitApplicationBarBehavior(你可以get it from NuGet)!