페이지를 스택에 푸시하지 않고 Xamarin.Forms에서 "위쪽"화살표를 표시하는 방법을 알아 보려고합니다. IE 뒤로 버튼을 눌렀을 때 작업을 수행하고 싶습니다. 나는 이것에 완전히 붙어 있으므로 어떤 도움을 주시면 감사하겠습니다.
라는보기 속성을 처리하는 사용자 지정 렌더러를 만들려고했습니다 DisplayHomeAsBack
. 렌더러에서 다음을 호출합니다.
FormsAppCompatActivity context = ((FormsAppCompatActivity)Forms.Context);
Android.Support.V7.App.ActionBar actionBar = context.SupportActionBar;
if (actionBar != null)
{
actionBar.SetDisplayHomeAsUpEnabled(element.DisplayHomeAsBack);
}
불행히도 안드로이드에 대한 모든 온라인 자습서와 stackoverflow 질문이이 방법을 제안하더라도 이것은 전혀 아무것도하지 않는 것 같습니다.
계획은 MasterDetailPage에서 "OnBackButtonPressed"재정의를 사용하여이 작업을 수행 할 수 있도록하는 것입니다. 불행히도 뒤로 버튼을 표시하는 것이 지금까지 더 큰 장애물이었습니다!
이 작업을 수행하는 더 좋은 방법이나 현재 메커니즘을 작동시키는 방법에 대한 아이디어가 있습니까?
편집하다
프로젝트를 만들고 도움이되는 경우 Xamarin 지원 포럼의이 질문에 업로드했습니다. http://forums.xamarin.com/discussion/comment/186330#Comment_186330
오래 기다리게해서 죄송합니다!
실제로이 코드를 실행하지 않고 내 자신의 코드에서 변경 했으므로 약간의 변경없이 완벽하게 작동하면 놀랄 것입니다.
따라서 아래에는 이전에 하나도 없었던 뒤로 버튼을 추가해야합니다 (예 : 실제로 돌아갈 페이지가없는 경우). 그런 다음 누를 때 수행 할 사용자 지정 작업을 추가합니다.
애니메이션을 사용하지 않고 새 페이지를 스택에 푸시하여 사용자에게 투명하고이 모든 것을 훨씬 더 간단하게 만들 것을 제안합니다.하지만 그렇게하고 싶지 않다면 아래 방법이 작동합니다.
MainActivity :
//Use this to subscribe to the event which will create the back button
public override bool OnCreateOptionsMenu(IMenu menu) {
if(menu != null && App.AppMasterPage != null) { //You will need this to make sure you are on your MasterDetailPage, just store a global reference to it in the App class or where ever
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
Xamarin.Forms.MessagingCenter.Subscribe<string>(this, "CreateBackButton", stringWeWillNotUse => { //Use this to subscribe to the event that creates the back button, then when you want the back button to show you just run Xamarin.Forms.MessagingCenter.Send<string>(this, "CreateBackButton")
ActionBar.DisplayOptions = ActionBarDisplayOptions.ShowTitle | ActionBarDisplayOptions.ShowHome | ActionBarDisplayOptions.UseLogo | ActionBarDisplayOptions.HomeAsUp; //You may need to play with these options to get it working but the important one is 'HomeAsUp' which should add the back button
});
} else {
Xamarin.Forms.MessagingCenter.Unsubscribe<string>(this, "CreateBackButton");
}
return base.OnCreateOptionsMenu(menu);
}
이제 다음 단계는 눌렀을 때 사용자 지정 작업을 수행하는 것입니다. 난 당신이 중 하나를 오버라이드 (override) 할 수 있다고 생각 OnBackPressed()
또는 OnOptionsItemSelected()
에 MainActivity
또는 어쩌면 당신은 대체 할 수 있습니다 MasterDetailPage
방법을. 잘 모르겠습니다.
어느 것이 당신에게 효과가 있든, 그 재정의 안에서 당신이 App.AppMasterPage
위에서했던 것처럼 당신이 당신에 있는지 확인 하고, 그렇다면 MessagingCenter
당신 App.AppMasterPage
이 이미 구독 한 메시지를 보내서 사용자 정의 작업을 처리 할 수 있습니다. .
막히면 알려주세요!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다