2017-06-02 32 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沒有得到預期的結果 - 當我試圖讓這段代碼最初工作時,我的圖像沒有正確改變,但是在完全從應用程序中移除測試設備後,圖像返回到正常。可能的緩存類型問題。 –

相關問題