2016-03-11 44 views
0

我正在使用xamarin.forms我的項目中有兩個輸入字段。我需要使用API發佈數據。我已經在mvc中創建了API。 MVVM用於我的表單項目,我有一個服務類和viewmodel發佈的數據,但我不能傳遞數據查看模型。 我的觀點xamarin格式的數據綁定

public class testapi : ContentPage 
{ 
    #region View Model 
    public TestViewModel ViewModel { get; set; } 
    #endregion 

    bool isNewItem; 
    Entry name; 
    Entry age; 

    #region Constructor 
    public testapi() 
    { 
     #region Binding Context 
     ViewModel = new TestViewModel(); 

     BindingContext = ViewModel.Testing; 
     #endregion 

     #region Using Triggers 

     var emptyValTrig = new EventTrigger(); 
     emptyValTrig.Event = "TextChanged"; 
     emptyValTrig.Actions.Add(new EmptyTextValidation()); 
     #endregion 
     name = new Entry 
     { 
      Keyboard = Keyboard.Text, 
      TextColor = Color.Black, 
      BackgroundColor = Color.Default, 
      Placeholder = "Name", 
      VerticalOptions = LayoutOptions.Center, 
      HorizontalOptions = LayoutOptions.FillAndExpand, 
     }; 
     name.SetBinding(Label.TextProperty, ("Name")); 
     name.Triggers.Add(emptyValTrig); 
     age = new Entry 
     { 
      Keyboard = Keyboard.Text, 
      TextColor = Color.Black, 
      BackgroundColor = Color.Default, 
      Placeholder = "Age", 
      VerticalOptions = LayoutOptions.Center, 
      HorizontalOptions = LayoutOptions.FillAndExpand, 

     }; 
     age.SetBinding(Label.TextProperty, ("Age")); 
     age.Triggers.Add(emptyValTrig); 

     Button loginButton = new Button 
     { 
      Text = "Login", 
      BorderRadius = 5, 
      TextColor = Color.White, 
      BackgroundColor = Color.Gray, 
      Command = ViewModel.SelectionChangedCommand 
     }; 
     var dataTrigger = new DataTrigger(typeof(Button)); 
     dataTrigger.Binding = new Binding("Text.Length", BindingMode.Default, source: name); 
     dataTrigger.Value = 0; 
     dataTrigger.Setters.Add(new Setter { Property = Button.IsEnabledProperty, Value = false }); 
     loginButton.Triggers.Add(dataTrigger); 

     var pageStack = new StackLayout(); 
     pageStack.Children.Add(name); 
     pageStack.Children.Add(age); 
     pageStack.Children.Add(loginButton); 
     pageStack.Spacing = 10; 
     Content = pageStack; 

    } 
    #endregion 

} 

和我的視圖模型

public class TestViewModel : BaseViewModel 
{ 
    // Testing = new test(); 
    public TestViewModel() 
    { 
     Testing = new test(); 

     SelectionChangedCommand = new Command(async() => 
     { 

      await RestaurantService.PostData<int, test>(RestaurantService.testdata, HttpMethod.Post, Testing).ContinueWith(test => 
      { 

      }, TaskScheduler.FromCurrentSynchronizationContext()); 
     }); 

    } 
    private test _test; 
    public test Testing 
    { 
     get { return _test; } 
     set 
     { 
      _test = value; 
      OnPropertyChanged("Testing"); 
     } 
    } 

    public System.Windows.Input.ICommand SelectionChangedCommand { get; set; } 
} 

和我有一個服務類太

class RestaurantService 
{ 
    public const string HostName = "http://192.168.0.44:100/api/"; 
    public const string item = "Order"; 
    public const string testdata = "Test"; 

    public static async Task<T> PostData<T, Tr>(string endpoint, HttpMethod method, 
                Tr content) 
    { 
     T returnResult = default(T); 
     HttpClient client = null; 
     try 
     { 
      client = new HttpClient(); 
      client.BaseAddress = new Uri(HostName); 
      client.DefaultRequestHeaders.Add("Accept", "application/json"); 
      client.Timeout = new TimeSpan(0, 0, 15); 
      HttpResponseMessage result = null; 
      StringContent data = null; 
      if (content != null) 
       data = new StringContent(JsonConvert.SerializeObject(content), UTF8Encoding.UTF8, "application/json"); 
      if (method == HttpMethod.Get) 
       result = await client.GetAsync(endpoint); 
      if (method == HttpMethod.Put) 
       result = await client.PutAsync(endpoint, data); 
      if (method == HttpMethod.Delete) 
       result = await client.DeleteAsync(endpoint); 
      if (method == HttpMethod.Post) 
       result = await client.PostAsync(endpoint, data); 
      if (result != null) 
      { 
       if (result.IsSuccessStatusCode 
            && result.StatusCode == System.Net.HttpStatusCode.OK) 
       { 
        var json = result.Content.ReadAsStringAsync().Result; 
        returnResult = JsonConvert.DeserializeObject<T>(json); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 

      Debug.WriteLine("Error fetching data: " + ex.Message); 
     } 
     finally 
     { 
      if (client != null) 
       client.Dispose(); 
     } 
     return returnResult; 

    } 
} 

我不能得到的數據,以我的視圖模型使用的API發佈。什麼我應該怎麼做

回答

0

看起來好像你是在你的頁面中綁定錯誤控件:

執行以下操作:

public class testapi : ContentPage 
{ 
#region View Model 
public TestViewModel ViewModel { get; set; } 
#endregion 

bool isNewItem; 
Entry name; 
Entry age; 

#region Constructor 
public testapi() 
{ 
    #region Binding Context 
    ViewModel = new TestViewModel(); 

    BindingContext = ViewModel.Testing; 
    #endregion 

    #region Using Triggers 

    var emptyValTrig = new EventTrigger(); 
    emptyValTrig.Event = "TextChanged"; 
    emptyValTrig.Actions.Add(new EmptyTextValidation()); 
    #endregion 
    name = new Entry 
    { 
     Keyboard = Keyboard.Text, 
     TextColor = Color.Black, 
     BackgroundColor = Color.Default, 
     Placeholder = "Name", 
     VerticalOptions = LayoutOptions.Center, 
     HorizontalOptions = LayoutOptions.FillAndExpand, 
    }; 
    name.SetBinding(Entry.TextProperty, ("Name")); 
    name.Triggers.Add(emptyValTrig); 
    age = new Entry 
    { 
     Keyboard = Keyboard.Text, 
     TextColor = Color.Black, 
     BackgroundColor = Color.Default, 
     Placeholder = "Age", 
     VerticalOptions = LayoutOptions.Center, 
     HorizontalOptions = LayoutOptions.FillAndExpand, 

    }; 
    age.SetBinding(Label.TextProperty, ("Age")); 
    age.Triggers.Add(emptyValTrig); 

    Button loginButton = new Button 
    { 
     Text = "Login", 
     BorderRadius = 5, 
     TextColor = Color.White, 
     BackgroundColor = Color.Gray, 
     Command = ViewModel.SelectionChangedCommand 
    }; 
    var dataTrigger = new DataTrigger(typeof(Button)); 
    dataTrigger.Binding = new Binding("Text.Length", BindingMode.Default, source: name); 
    dataTrigger.Value = 0; 
    dataTrigger.Setters.Add(new Setter { Property = Button.IsEnabledProperty, Value = false }); 
    loginButton.Triggers.Add(dataTrigger); 

    var pageStack = new StackLayout(); 
    pageStack.Children.Add(name); 
    pageStack.Children.Add(age); 
    pageStack.Children.Add(loginButton); 
    pageStack.Spacing = 10; 
    Content = pageStack; 

} 
#endregion 

}

name.SetBinding(Entry.TextProperty,( 「名字」));

age.SetBinding(Entry.TextProperty,(「age」));

乾杯!