好吧,这与其说是个问题,不如说是个烦恼。没有错误
页面
<ContentPage
...
x:Name="This"
//hack to have typed xaml at design-time
BindingContext="{Binding Source={x:Static viewModels:ViewModelLocator.ChooseTargetLocationVm}}"
subview
<views:ProductStandardView
...
BindingContext="{Binding Product}">
<Grid.Triggers>
<DataTrigger
Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"
TargetType="Grid"
Value="true">
<Setter Property="BackgroundColor" Value="{StaticResource WarningColor}" />
</DataTrigger>
</Grid.Triggers>
当绑定(bind)到BindingContext来自 This 的来源 引用 ,我收到 XAML“警告”
Cannot resolve property 'IsVacate' in data context of type 'object'
Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"
很明显,BindingContext 是一个对象 并且是未类型化的。 但是上面的代码编译和工作
我想做的是转换,首先是因为我有强制症,但主要是因为它很容易在IDE页面 channel 栏上发现真正的问题
以下看似合乎逻辑但行不通
Binding="{Binding Path=BindingContext.(viewModels:ChooseTargetLocationVm.IsVacate),
Source={x:Reference This}}"
在我得到的输出中
[0:] Binding: '(
viewModels:ChooseTargetLocationVm' property not found on 'Inhouse.Mobile.Standard.ViewModels.ChooseTargetLocationVm', target property: 'Inhouse.Mobile.Standard.Views.ProductStandardView.Bound'
我明白这个错误,但我还能怎么投?
只是为了愚蠢,显然以下不会编译
Binding="{Binding Path=((viewModels:ChooseTargetLocationVm)BindingContext).IsVacate, Source={x:Reference This}}"
那么有没有一种方法可以将 BindingContext 转换为 ViewModel,以便在设计时输入任何 SubProperty 引用?
更新
这与 DataTemplate 内部相关或者在这种情况下,控件有自己的 BindingContext这就是为什么我需要使用 Source={x:Reference This}定位页面。
注意:<ContentPage.BindingContext>对我不起作用,因为我使用的是 prism 和 unity,而且在初始测试中它似乎不能很好地使用默认构造函数,尽管我可能会更多地使用它
最佳答案
您可以扩展 ContentPage 以创建一个通用类型 - 支持 View 模型的类型参数 - 进而可以在 Binding 标记扩展中使用。
虽然它可能不会为您提供智能感知之类的支持 - 但绝对应该为您删除警告。
例如:
/// <summary>
/// Create a base page with generic support
/// </summary>
public class ContentPage<T> : ContentPage
{
/// <summary>
/// This property basically type-casts the BindingContext to expected view-model type
/// </summary>
/// <value>The view model.</value>
public T ViewModel { get { return (BindingContext != null) ? (T)BindingContext : default(T); } }
/// <summary>
/// Ensure ViewModel property change is raised when BindingContext changes
/// </summary>
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
OnPropertyChanged(nameof(ViewModel));
}
}
<?xml version="1.0" encoding="utf-8"?>
<l:ContentPage
...
xmlns:l="clr-namespace:SampleApp"
x:TypeArguments="l:ThisPageViewModel"
x:Name="This"
x:Class="SampleApp.SampleAppPage">
...
<Label Text="{Binding ViewModel.PropA, Source={x:Reference This}}" />
...
</l:ContentPage>
代码隐藏
public partial class SampleAppPage : ContentPage<ThisPageViewModel>
{
public SampleAppPage()
{
InitializeComponent();
BindingContext = new ThisPageViewModel();
}
}
查看模型
/// <summary>
/// Just a sample viewmodel with properties
/// </summary>
public class ThisPageViewModel
{
public string PropA { get; } = "PropA";
public string PropB { get; } = "PropB";
public string PropC { get; } = "PropC";
public string[] Items { get; } = new[] { "1", "2", "3" };
}
关于c# - 转换绑定(bind)路径,以便它在设计时识别 ViewModel 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51412780/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs