gpt4 book ai didi

c# - 如何在 Windows Phone 8 上进行代码内绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 16:13:39 25 4
gpt4 key购买 nike

我正在尝试在 Windows Phone 8 上执行代码内绑定(bind),例如“如何在 Windows Phone 上执行 CreateBindingSet()?”这个问题。 How to do CreateBindingSet() on Windows Phone?我想我已经按照 Stuart 的建议完成了这些步骤,但我不断收到异常错误或输出错误“MvxBind:警告:9,86 无法为 TextUpdate 的绑定(bind) _update 创建目标绑定(bind)”在 Droid 中它工作得很好,那么我做错了什么,遗漏了或没有看到?有什么建议么?我在电话部分进行了以下设置。

设置.cs:

using Cirrious.CrossCore.Platform;
using Cirrious.MvvmCross.BindingEx.WindowsShared;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.WindowsPhone.Platform;
using Microsoft.Phone.Controls;

namespace DCS.Phone
{
public class Setup : MvxPhoneSetup
{
public Setup(PhoneApplicationFrame rootFrame) : base(rootFrame)
{
}

protected override IMvxApplication CreateApp()
{
return new Core.App();
}

protected override IMvxTrace CreateDebugTrace()
{
return new DebugTrace();
}

protected override void InitializeLastChance()
{
base.InitializeLastChance();

var builder = new MvxWindowsBindingBuilder();
builder.DoRegistration();
}
}
}

服务器 View .xaml:

<views:MvxPhonePage
x:Class="DCS.Phone.Views.ServerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:Cirrious.MvvmCross.WindowsPhone.Views;assembly=Cirrious.MvvmCross.WindowsPhone"
xmlns:converters="clr-namespace:DCS.Phone.Converters"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->

<Grid x:Name="LayoutRoot" Background="Transparent">
<!--used as dummy bool to call Update callback through converter -->
<CheckBox x:Name ="DummyUpdated" Height ="0" Width ="0" Content="" Visibility="Visible" IsChecked="{Binding TextUpdate, Converter={StaticResource Update }, ConverterParameter=ServerView}"></CheckBox>
<ScrollViewer x:Name="ScrollView" Height="760" Width ="480" VerticalAlignment="Top">
<Canvas x:Name="View" Top="0" Left="0" Margin ="0" Background="Transparent" Height="Auto" Width ="Auto"/>
</ScrollViewer>
</Grid>
</views:MvxPhonePage>

ServerView.xaml.cs:

using System.Windows;
using Cirrious.MvvmCross.Binding.BindingContext;
using Cirrious.MvvmCross.WindowsPhone.Views;
using DCS.Core;
using DCS.Core.ViewModels;
using DCS.Phone.Controls;

namespace DCS.Phone.Views
{
public partial class ServerView : MvxPhonePage,IMvxBindingContextOwner
{
private bool _isUpdating;
private bool _update;
private DcsText _text;
private DcsInput _input;
private DcsList _list;
private DcsButton _button;

public IMvxBindingContext BindingContext { get; set; }

public ServerView(){
InitializeComponent();
BindingContext = new MvxBindingContext();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}

private void MainPage_Loaded(object sender, RoutedEventArgs e){
CreateControls();
}
private void CreateControls() {
//User controls
_text = new DcsText(View);
_input = new DcsInput(View, View, DummyUpdated);
_list = new DcsList(View);
_button = new DcsButton(View);

//Bindings
var set = this.CreateBindingSet<ServerView, ServerViewModel>();
set.Bind(this).For(v => v._update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
for (int i = 0; i < Constants.MaxButton; i++){
set.Bind(_button.Button[i]).To(vm => vm.ButtonCommand).CommandParameter(i);
}
set.Apply();
AppTrace.Trace(string.Format("OnCreate Finish"));
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e){
base.OnNavigatedTo(e);
BindingContext.DataContext = this.ViewModel;
}
}
}

当我尝试执行 set.Apply(); 时出现以下错误

set.Apply() 异常;

var set = this.CreateBindingSet<ServerView, ServerViewModel>();
for (int i = 0; i < Constants.MaxButton; i++){
set.Bind(_button.Button[i]).To(vm => vm.ButtonCommand).CommandParameter(i);
}
set.Apply();

System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Cirrious.MvvmCross.BindingEx.WindowsPhone
StackTrace:
at Cirrious.MvvmCross.BindingEx.WindowsShared.MvxDependencyPropertyExtensionMethods.EnsureIsDependencyPropertyName(String& dependencyPropertyName)
at Cirrious.MvvmCross.BindingEx.WindowsShared.MvxDependencyPropertyExtensionMethods.FindDependencyPropertyInfo(Type type, String dependencyPropertyName)
at Cirrious.MvvmCross.BindingEx.WindowsShared.MvxDependencyPropertyExtensionMethods.FindDependencyProperty(Type type, String name)
at Cirrious.MvvmCross.BindingEx.WindowsShared.MvxBinding.MvxWindowsTargetBindingFactoryRegistry.TryCreatePropertyDependencyBasedBinding(Object target, String targetName, IMvxTargetBinding& binding)
at Cirrious.MvvmCross.BindingEx.WindowsShared.MvxBinding.MvxWindowsTargetBindingFactoryRegistry.TryCreateReflectionBasedBinding(Object target, String targetName, IMvxTargetBinding& binding)
at Cirrious.MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.CreateBinding(Object target, String targetName)
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding(Object target)
at Cirrious.MvvmCross.Binding.Bindings.MvxFullBinding..ctor(MvxBindingRequest bindingRequest)
at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle(MvxBindingRequest bindingRequest)
at Cirrious.MvvmCross.Binding.Binders.MvxFromTextBinder.<>c__DisplayClass1.<Bind>b__0(MvxBindingDescription description)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings(IMvxBindingContextOwner view, IEnumerable`1 bindings, Object clearKey)
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings(IMvxBindingContextOwner view, Object target, IEnumerable`1 bindingDescriptions, Object clearKey)
at Cirrious.MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding(IMvxBindingContextOwner view, Object target, MvxBindingDescription bindingDescription, Object clearKey)
at Cirrious.MvvmCross.Binding.BindingContext.MvxBaseFluentBindingDescription`1.Apply()
at Cirrious.MvvmCross.Binding.BindingContext.MvxFluentBindingDescriptionSet`2.Apply()
at DCS.Phone.Views.ServerView.CreateControls()
at DCS.Phone.Views.ServerView.MainPage_Loaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
InnerException:

在 set.Apply() 处输出;

var set = this.CreateBindingSet<ServerView, ServerViewModel>();
set.Bind(this).For(v => v._update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
set.Apply();

MvxBind:Warning: 9,86 Failed to create target binding for binding _update for TextUpdate

只是澄清一下。

set.Bind(this).For(v => v._update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);

在 set.Apply() 上发出警告

MvxBind:Warning:  9,86 Failed to create target binding for binding _update for TextUpdate

使用 public bool Update 确实解决了 set.Apply() 问题,但我没有得到绑定(bind)。

在 Droid 中所有这些都在工作

set.Bind(this).For("_update").To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
set.Bind("_update").To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
set.Bind(this).For(v=>v._update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
set.Bind(this).For(v=>v.Update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
set.Bind("Update").To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);

set.Bind(_button.Button[i]).To(vm => vm.ButtonCommand).CommandParameter(i);

给出异常(exception):

最佳答案

查看堆栈跟踪,代码失败:https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.BindingEx.WindowsPhone/MvxDependencyPropertyExtensionMethods.cs#L113

    private static void EnsureIsDependencyPropertyName(ref string dependencyPropertyName)
{
if (!dependencyPropertyName.EndsWith("Property"))
dependencyPropertyName += "Property";
}

因此这表明传递给方法的 dependencyPropertyName 名称是 null

我的猜测是,这是因为 _update 不是 属性并且不是 public。 MvvmCross 绑定(bind)适用于属性 - .Net 安全性强制它只能与 public 一起使用(尽管您可以使用 internal 和一点 assembly:InternalsVisibleTo代码)。

尝试将 _update 替换为具有 get 和 set 访问权限的 public 属性 - 例如:

  public bool Update {
get { return _update; }
set { _update = value; /* do something with the new value? */ }
}

与:

 set.Bind(this).For(v => v.Update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);

旁白:在非 Windows 绑定(bind)中,此问题会导致更好的错误消息 - Empty binding target passed to MvxTargetBindingFactoryRegistry from https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/Bindings/Target/Construction/MvxTargetBindingFactoryRegistry.cs#L34 - 也将列入 Windows 的待办事项列表。

关于c# - 如何在 Windows Phone 8 上进行代码内绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21419043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com