2016-09-08 2 views
1

LobbyView 및 LobbyViewModel이 MvvmCross for Android를 사용하여 구현되었습니다.mvvmcross의 ViewModel 업데이트에서 Android ActionBar를 숨기는 방법?

LobbyViewModel에는 사용자를 인증하는 데 사용되는 LoginCommand가 있습니다. 그리고 사용자가 인증되면 LobbyView에 정의 된 작업 표시 줄을 숨길 필요가 있습니다

viewmodel에서 actionbar.Hide()에 액세스하는 방법을 모르거나 액세스 할 수있는 다른 방법이 있습니다. . 뷰 모델에 대한

코드 : 로비보기위한

public class LobbyViewModel : MvxViewModel 
{ 
    ISmartfoxService _smarfoxservice; 
    private string _username; 
    private string _password; 
    private string _title = "Connect To SfsServer"; 
    private bool _LoginSuccces = false; 
    private string _LoginVisibility = "VISIBLE"; 
    private int _ListVisibility = 8; 
    private string _LoginError = "Connecting..."; 
    public LobbyViewModel(ISmartfoxService smartfoxservice) 
    { 
     _smarfoxservice = smartfoxservice; 
    } 


    public string UserName 
    { 
     get { return _username; } 
     set { SetProperty(ref _username, value); } 
    } 
    public string Pass 
    { 
     get 
     { 
      return _password; 
     } 
     set 
     { 
      SetProperty(ref _password, value); 
     } 
    } 
    public string Title 
    { 
     get { return _title; } 
     set { SetProperty(ref _title, value); } 
    } 
    public bool LoginSuccess 
    { 
     get { return _LoginSuccces; } 
     set 
     { 

      _LoginSuccces = value; 
      RaisePropertyChanged(() => LoginSuccess); 
      if (value == true) 
      { 
       LoginVisibility = "Gone"; 

      } 
     } 
    } 
    public string LoginVisibility 
    { 
     get { return _LoginVisibility; } 
     set { _LoginVisibility = value; RaisePropertyChanged(() => LoginVisibility); } 
    } 


    public int ListVisibility 
    { 
     get { return _ListVisibility; } 
     set { _ListVisibility = value; RaisePropertyChanged(() => ListVisibility); } 
    } 
    public string LoginError 
    { 
     get { return _LoginError; } 
     set { _LoginError = value; RaisePropertyChanged(() => LoginError); } 
    } 

    // A Sample Login Command which notifies the Lobbyviewmodel about login status 
    public ICommand LoginCommand 
    { 
     get 
     { 
      return new MvxCommand(() => 
      { 
       IPhpService service = new PHPService(); 
       LoginResponse result = await service.TryLogin(UserName,Pass); 
       if(result.isLogged==true) 
       { 
        LoginSuccess=true; 
        // Here, I would to like call the android action bar's Hide method of LobbyView. 
       } 
       else 
        LoginSuccess=false; 
      }); 
     } 

    } 
} 

코드 :

public class LobbyView : MvxActivity 
{ 
    public CustomAdapter customAdapter; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.LobbyView); 
     var set = this.CreateBindingSet<LobbyView, LobbyViewModel>(); 
     Button btnConnect = (Button)FindViewById(Resource.Id.btnlogin); 
     EditText edtUserName = (EditText)FindViewById(Resource.Id.edtlogin); 
     EditText edtPassword= (EditText)FindViewById(Resource.Id.edtpassword); 
     TextView txtErrorMessage = (TextView)FindViewById(Resource.Id.txtloginerror); 
     set.Bind(txtErrorMessage).For(tr => tr.Text).To(vm => vm.LoginError); 
     set.Bind(edtUserName).For("Text").To(vm => vm.UserName); 
     set.Bind(edtPassword).For("Text").To(vm => vm.Pass); 
     set.Bind(btnConnect).For("Text").To(vm => vm.Title); 
     set.Bind(btnConnect).For("Click").To(vm => vm.LoginCommand); 
     set.Apply(); 

     ActionBar actionBar = this.ActionBar; 
     actionBar.SetDisplayShowHomeEnabled(false); 
     actionBar.SetDisplayShowTitleEnabled(false); 
     LayoutInflater inflater = LayoutInflater.From(this); 
     View mCustomeView=inflater.Inflate(Resource.Layout.custom_action_bar,null); 
     actionBar.SetDisplayOptions(ActionBarDisplayOptions.ShowCustom, ActionBarDisplayOptions.ShowHome); 
     actionBar.SetCustomView(Resource.Layout.custom_action_bar); 
     actionBar.SetDisplayShowCustomEnabled(true); 

    } 
} 
+0

마찬가지로 사이드 바 : ActionBar 대신 툴바가 사용됩니다. 또한 MvxAppCompatActivity – Martijn00

+0

을 사용해야합니다. set.Bind 대신 Android에서 xml을 직접 바인딩 할 수 있습니다. – Martijn00

+0

(단추) FindViewById 캐스팅은 Xamarin에서 권장되지 않습니다. 대신 FindViewById

답변

1

당신이 할 수있는 것은 :

ViewModel.PropertyChanged += (sender, e) => 
{ 
    if (e.PropertyName == "LoginSuccess" && ViewModel.LoginSuccess) 
    { 
     //Hide Actionbar here 
    } 
} 

당신이 일반적인를 추가해야 ViewModel에 입력 한 이름을 사용하려면 보기 모델 :

public class LobbyView : MvxActivity<LobbyViewModel> 
1

@ Martijn00 답변에 대한 대안으로, 특정 속성이 변경된 경우 이벤트를 약하게 구독하는 것입니다.

_loginSuccessSubscription = ViewModel.WeakSubscribe(() => ViewModel.LoginSucess, LoginSuccessChanged); 

private void LoginSuccessChanged(object sender, PropertyChangedEventArgs args) 
{ 
    if (ViewModel.LoginSuccess) 
    { 
     // do stuff on login... 

     // Hide ToolBar (this in this context is the Activity or Fragment) 
     this.SupportActionbar.Hide(); 
    } 
} 

이 방법은 이벤트에 대한 강한 참조가없는 당신은 조금 더 쉽게 휴식 할 수 있습니다.

관련 문제