2017-06-02 60 views
1

我试图在更改ImageSource时更新/重新绘制图像,这将帮助我重新加载异步。Xamarin.Forms将SetBinding设置为Image,以便在Source更改时刷新

我认为绑定到图像的imageSource具有绑定属性是一个开始,但它不更新图像。我尝试了很多方法,包括使用OnPropertyChanged事件的viewModel方法,但我不认为我完全理解这一点。

此绑定也必须在代码中完成,这就是应用程序如何写入(最小xaml)。

到目前为止,我一般的途径是:

绑定属性

public static readonly BindableProperty ImageFileProperty = BindableProperty.Create("ImageProperty", typeof(string), typeof(CustomImageClass));

public string ImageProperty { get { return (string)GetValue(ImageFileProperty); } set { SetValue(ImageFileProperty, value); } }

里面的CustomImageClass构造:

this.SetBinding(ImageFileProperty, "ImageProperty");

从这里我想在更新ImageSource并更改图像时更改图像。我希望这足够具体,我认为绑定到xaml的所有不同示例都让我感到困惑,因为我需要在代码中执行这些示例。

回答

0

对不起的英语太差

我想有很多的问题在这里...

如果我没有理解好了,你想在你的CustomImageClass创建BindableProperty。是对的吗? 如果是的话,所以你可以使用默认的惯例到绑定属性名的,就像这样(请注意,我改变类型太多):

public static readonly BindableProperty ImageFileProperty = 
     BindableProperty.Create("ImageFile", typeof(ImageSource), 
     typeof(CustomImageClass)); 

public ImageSource ImageFile 
{ 
    get{ return (string)GetValue(ImageFileProperty); } 
    set{ SetValue(ImageFileProperty, value); } 
} 

不能设置绑定到这个属性,你刚刚在你的构造函数中创建。这将在您使用类(在xaml或c#中)时使用。

现在你必须使用你的财产来设置你想要显示的图像。我想你应该有在这一类,它是图像类型,例如“形象”的变量或私有财产,所以,在构造函数中,你应该做

image = new Image(); 
image.BindingContext = this; 
image.SetBinding(Image.Source, nameof(ImageFile)); 

让我知道如果我missunderstood,请。 我希望能帮到你。

+0

非常感谢迭戈!没有你的英语解释是好的。 我并没有正确添加BindingContext,因为您已经为我指出了正确的方法。这现在对我来说很好。 另一件需要注意的事情是PropertyBindings没有得到预期的结果 - 当我试图让这段代码最初工作时,我的图像没有正确改变,但是在完全从应用程序中移除测试设备后,图像返回到正常。可能的缓存类型问题。 –

相关问题