Reputation: 51
I'm trying to do in-code binding on Windows Phone 8, like in the question "How to do CreateBindingSet() on Windows Phone?" How to do CreateBindingSet() on Windows Phone? I think I have done the steps as suggested by Stuart, but I keep getting exception error or output error "MvxBind:Warning: 9,86 Failed to create target binding for binding _update for TextUpdate" In Droid it works very well, so what am I doing wrong, missing or don't see? Any suggestions? I have the following setup in the Phone part.
Setup.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();
}
}
}
ServerView.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;
}
}
}
I get the following errors when I try to do set.Apply();
Exception at 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:
Output at 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
Just to clerify.
set.Bind(this).For(v => v._update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
gave the warning on set.Apply()
MvxBind:Warning: 9,86 Failed to create target binding for binding _update for TextUpdate
Using the public bool Update did solve the set.Apply() problem, but I don't get the binding.
In Droid all these are working
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);
gave the exception:
Upvotes: 3
Views: 937
Reputation: 66882
Looking at the stack trace, the code is failing on: 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";
}
So this would suggest that the dependencyPropertyName
name being passed to the method is null
.
My guess is that this is because _update
is not a property and not public
. MvvmCross binding works on properties - and .Net security forces it to only work with public
(although you could use internal
with a little assembly:InternalsVisibleTo
code).
Try replacing _update
with a public
property with both get and set access - e.g.:
public bool Update {
get { return _update; }
set { _update = value; /* do something with the new value? */ }
}
with:
set.Bind(this).For(v => v.Update).To(vm => vm.TextUpdate).OneWay().WithConversion("Update", this);
Aside: In non-Windows binding this problem would result in a much nicer error message - 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 - will put on the todo list for Windows too.
Upvotes: 3