c# - 我可以在 WinRT/Windows 8 商店应用程序中绑定(bind)动态对象吗

标签 c# .net dynamic

我有以下代码:

    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.
    }

当我从 xaml 对其进行绑定(bind)时,TryGetMember 中的调试点不是触发器。我错过了什么吗?

<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>

数据上下文设置为

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

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

最佳答案

这是我用于某种动态绑定(bind)的解决方案。 绑定(bind)语法只需要包含 []:

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);
        }
    }
}

关于c# - 我可以在 WinRT/Windows 8 商店应用程序中绑定(bind)动态对象吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12894952/

相关文章:

c# - 异步任务超时

c# - HangFire 重复任务数据

c# - 通过在 C# 中传递 null 的选项将枚举传递给函数的简洁方法

.net - 如何在 PowerShell 中设置文化?

c# - 如何在 ASP.NET Core 2.1 网站上启用 HttpOnly cookie

c# - Yield 方法中的垃圾收集

c# - 将具有类型参数的复杂类型作为 CascadeParameter 传递给子 RenderFragment

javascript - 动态 JavaScript 函数声明

python - 在Python中为split()创建动态 "variable layout"

javascript - 动态模块加载在 Firefox 上不起作用 - SyntaxError : dynamic module import is not implemented