Dynamic Object Binding Behavior Different between WPF and WinRT

I am trying to bind data to a Xaml property to a property of a dynamic object. In this case, the dynamic object is JObjectfrom the Json.Net library . JObjectis a well-managed dynamic object, and in the past I had no problems with it. This makes it easy to get to / from serialized json objects and C # code.

In this case, however, the binding throws an exception at runtime.

Binding:

 <TextBlock Text="{Binding UserProfile.Value.name}"/>

where Value- it JObjectand namethe dynamic property (string)

An exception:

    Error: BindingExpression path error: 'name' property not found on 'Newtonsoft.Json.Linq.JObject, 
Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. 
BindingExpression: Path='UserProfile.Value.name' DataItem='Google.Apps.ViewModel.MainViewModel'; 
target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' 
(type 'String')

Curious if I explicitly convert a JObject into an ExpandoObject object that works with binding.

private dynamic _value; // set to a deserialized JObject elsewhere
public dynamic Value
{
    get
    {
        if (_value != null) // debug code
        {
            dynamic o = new ExpandoObject();
            o.name = _value.name;
            return o;
        }
        return _value;
    }
}

ExpandoObject. , . case ExpandoObject ExpandoObject ?

UPDATE

@dkozl, WPF. WPF :

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="root"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock FontSize="36" Width="920">Label</TextBlock>
            <TextBlock Text="{Binding Path=TheName.name, ElementName=root}" FontSize="36"/>
        </StackPanel>
    </Grid>
</Window>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public dynamic TheName
    {
        get
        {
            string json = @"{ 'name': 'James' }";
            return JsonConvert.DeserializeObject<dynamic>(json);
        }
    }
}

WinRT 8.1 Store :

<Page
    x:Name="root"
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

  <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel Orientation="Vertical">
      <TextBlock FontSize="36" >Label</TextBlock>
      <TextBlock Text="{Binding Path=TheName.name, ElementName=root}" FontSize="36"/>
    </StackPanel>
  </Grid>
</Page>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    public dynamic TheName
    {
        get
        {
            string json = @"{ 'name': 'James' }";
            return JsonConvert.DeserializeObject<dynamic>(json);
        }
    }
}

, . , WinRT.

+4

Source: https://habr.com/ru/post/1542082/


All Articles