我可以绑定反对的WinRT / Windows 8的应用程序商店一DynamicObject [英] Can i bind against a DynamicObject in WinRT / Windows 8 Store Apps

查看:123
本文介绍了我可以绑定反对的WinRT / Windows 8的应用程序商店一DynamicObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

 公共类MyClass的:DynamicObject,INotifyPropertyChanged的
    {
    字典<字符串,对象>性能=新字典<字符串,对象>();

    公众覆盖布尔TryGetMember(GetMemberBinder粘合剂,out对象结果)
    {
        如果(properties.ContainsKey(binder.Name))
        {
            结果=性能[binder.Name]
            返回true;
        }
        其他
        {
            结果=无效的属性!;
            返回false;
        }
    }

    公众覆盖布尔TrySetMember(SetMemberBinder粘结剂,对象值)
    {
        性能[binder.Name] =值;
        this.OnPropertyChanged(binder.Name);
        返回true;
    }

    公众覆盖布尔TryInvokeMember(InvokeMemberBinder粘结剂,对象[] ARGS,out对象结果)
    {
        动态方法=特性[binder.Name]
        结果=方法(参数[0]的ToString()的args [1]的ToString());
        返回true;
    }

    /// ....之类的休息。
    }
 

当我绑定反对从XAML,在TryGetMember调试点不triggeret。我是否错过了什么?

 < D​​ataTemplate中X:关键=SearchResult所>
    <电网WIDTH =294保证金=6>
        < Grid.ColumnDefinitions>
            < ColumnDefinition宽度=自动/>
            &所述; ColumnDefinition宽度=*/>
        < /Grid.ColumnDefinitions>
        < BORDER背景={的StaticResource ListViewItemPlaceholderBackgroundThemeBrush}保证金=0,0,0,10WIDTH =40高度=40>
            <图像源={绑定路径=横幅}拉伸=UniformToFill/>
        < /边框>
        &所述;的StackPanel Grid.Column =1保证金=10,-10,0,0>
            < TextBlock的文本={结合SeriesName}风格={的StaticResource BodyTextStyle}TextWrapping =NoWrap的/>
            < TextBlock的文本={结合字幕}风格={的StaticResource BodyTextStyle}前景={的StaticResource ApplicationSecondaryForegroundThemeBrush}TextWrapping =NoWrap的/>
            < TextBlock的文本={绑定概述}风格={的StaticResource BodyTextStyle}前景={的StaticResource ApplicationSecondaryForegroundThemeBrush}TextWrapping =NoWrap的/>
        < / StackPanel的>
    < /网格>
< / DataTemplate中>
 

在DataContext设置为

 公开的ObservableCollection<动态> SearchResult所{获取;集;}
 

  ICollection的山坳= item.SearchResults;
 this.DefaultViewModel [结果] =关口; //这是在GridView的的datacontext
 

解决方案

下面是动态绑定的那种,我使用的是解决方案。 绑定语法只需要包括】:     

 公共类DynamicLocalSettings:BindableBase
{
    公共对象本[字符串名称]
    {
        得到
        {
            如果(ApplicationData.Current.LocalSettings.Values​​.ContainsKey(名))
                回报ApplicationData.Current.LocalSettings.Values​​ [名]
            返回null;
        }
        组
        {
            ApplicationData.Current.LocalSettings.Values​​ [名] =值;
            OnPropertyChanged(名称);
        }
    }
}
 

I have the following code:

    public class MyClass: DynamicObject, INotifyPropertyChanged
    {
    Dictionary<string, object> properties = new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (properties.ContainsKey(binder.Name))
        {
            result = properties[binder.Name];
            return true;
        }
        else
        {
            result = "Invalid Property!";
            return false;
        }
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        properties[binder.Name] = value;
        this.OnPropertyChanged(binder.Name);
        return true;
    }

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        dynamic method = properties[binder.Name];
        result = method(args[0].ToString(), args[1].ToString());
        return true;
    }

    ///.... Rest of the class.
    }

When i bind against it from xaml, debug points in TryGetMember is not triggeret. Do i miss something?

<DataTemplate x:Key="SearchResults">
    <Grid Width="294" Margin="6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,0,0,10" Width="40" Height="40">
            <Image Source="{Binding Path=Banner}" Stretch="UniformToFill"/>
        </Border>
        <StackPanel Grid.Column="1" Margin="10,-10,0,0">
            <TextBlock Text="{Binding SeriesName}" Style="{StaticResource BodyTextStyle}" TextWrapping="NoWrap"/>
            <TextBlock Text="{Binding Subtitle}" Style="{StaticResource BodyTextStyle}" Foreground="{StaticResource ApplicationSecondaryForegroundThemeBrush}" TextWrapping="NoWrap"/>
            <TextBlock Text="{Binding Overview}" Style="{StaticResource BodyTextStyle}" Foreground="{StaticResource ApplicationSecondaryForegroundThemeBrush}" TextWrapping="NoWrap"/>
        </StackPanel>
    </Grid>
</DataTemplate>

The datacontext is set to

public ObservableCollection<dynamic> SearchResults {get;set;}

and

 ICollection col = item.SearchResults;    
 this.DefaultViewModel["Results"] = col;  //this is the datacontext of the gridview

解决方案

Here is a solution I am using for kind-of dynamic binding. The binding syntax just needs to include []:

public class DynamicLocalSettings : BindableBase
{
    public object this[string name]
    {
        get
        {
            if (ApplicationData.Current.LocalSettings.Values.ContainsKey(name))
                return ApplicationData.Current.LocalSettings.Values[name];
            return null;
        }
        set
        {
            ApplicationData.Current.LocalSettings.Values[name] = value;
            OnPropertyChanged(name);
        }
    }
}

这篇关于我可以绑定反对的WinRT / Windows 8的应用程序商店一DynamicObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆