簡體   English   中英

Windows Phone 8異步Web請求與mvvm

[英]windows phone 8 async web request with mvvm

我在應用程序中使用mvvm設計模式。 在視圖上單擊按鈕時,它將在我的視圖模型中吃午飯,然后從視圖模型中吃午餐時在模型中另一個方法通過Web請求從Web地址獲取內容。 由於Web請求是一種異步方法,因此我實現了一個Observer設計模式,以將請求的響應從數據服務推送到我的視圖模型。

2個問題:

  1. 這是個好主意嗎 ?
  2. 如何將響應從我的視圖模型推送到視圖? 第二個觀察員dp?

謝謝。

編輯 :我更改我的代碼以使用INotifyPropertyChanged,但我仍然阻止視圖的更新...

在我看來 :

private void searchButton_Click(object sender, RoutedEventArgs e)
{
    vm = new ResultSearchViewModel();
    vm.Search(stringRequest);

    DataContext = vm.ResponseParse;
}

在我看來,模型:

class ResultSearchViewModel
{
    private string _response
    {
        get { return App.WR.Result; }
    }

    public ContainerResult ResponseParse
    {
        get 
        {
            if (_response != null)
            {
                return JsonConvert.DeserializeObject<ContainerResult>(_response);
            }
            else
            {
                return null;
            }
        }
    }

    public void Search(string searchContent)
    {
        App.WR.Get("http://api.deezer.com/search?q=" + searchContent);
    }
}

App.WR是我在應用程序中提供的服務:

public class WebRequestService : INotifyPropertyChanged
{
    private string result;
    public string Result
    {
        get { return result; }
        set
        {
            result = value;
            NotifyPropertyChanged("Result");
        }
    }

    public string Uri;
    public HttpWebRequest Request;


    public void Get(string _uri)
    {
        Uri = _uri;

        Request = (HttpWebRequest)HttpWebRequest.Create(Uri);
        Request.BeginGetResponse(GetStringFromStream, Request);
    }

    private void GetStringFromStream(IAsyncResult result)
    {
        HttpWebRequest request = result.AsyncState as HttpWebRequest;
        if (request != null)
        {
            try
            {
                WebResponse response = request.EndGetResponse(result);

                try
                {
                    StreamReader streamReader = new StreamReader(response.GetResponseStream(), true);
                    try
                    {
                        Result = streamReader.ReadToEnd();
                    }
                    finally
                    {
                        streamReader.Close();
                    }
                }
                catch (WebException e)
                {
                    Result = e.ToString();
                }
                finally
                {
                    response.Close();
                }
            }
            catch (WebException e)
            {
                Result = e.ToString();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Web請求可以正常工作,如果我添加som Debug.WriteLine,我可以正確地看到Json Parse,但是我真的不知道如何更新視圖...如果有人知道該怎么做,我將不勝感激:)

我通常希望看到的結果是:

  • 視圖上的按鈕綁定到ViewModel中的Command。
  • 命令在注入的服務上執行方法,或直接在模型中執行請求數據的方法。
  • 響應用於填充VM的一個或多個屬性。
  • 該屬性綁定到視圖,以便在那里顯示更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM