다소 복작하고 어려울수 있으나 일단 한번 따라서 완성 시키시고 나면 금방 이해가 가실거라 생각합니다
1. 프로젝트 생성
- 비쥬얼스튜디오 2017환경에서 개발하엿습니다 참고해서 봐주세요
- 파일 -> 새로만들기 -> 프로젝트
- 프로젝트 형식은 Cross-Platform 형식이며, 모바일 앱(Xamarin.Forms) 형식의 폼을 구성해서 만듭니다
- Cross Platform의 세부 설정입니다 빈화면에 호환되는 플랫폼의 종류 등을 설정 합니다
2. 폼 구성하기
- Cross Platform 솔루션 구성을 먼저 이해해야 합니다
- 전체 프로젝트를 감싸는 솔루션 밑에 플랫폼을 관장해야할 C# 프로젝트가 있고
그 밑에 각 플렛폼에 따른 프로젝트가 있습니다
- 프로젝트명.Droid : 안드로이드 플렛폼
- 프로젝트명.IOS : 애플 플렛폼
- 프로젝트폄.UWP : 테블릿 플렛폼
- 저희가 수정할 부분은 C# 프로젝트 입니다
- C#프로젝트 부분에서 App.xaml 파일을 마우스 오른쪽 버튼을 눌러 삭제 합니다
- C# 프로젝트에서 마우스 오른쪽 버튼을 통해 새항목을 선택합니다
- App.cs 클래스 파일을 추가합니다
- App.cs 클래스 파일에 다음과 같이 코딩합니다
*App.cs
using Xamarin.Forms;
namespace LoginNavigation
{
public class App : Application
{
public static bool IsUserLoggedIn { get; set; }
public App ()
{
if (!IsUserLoggedIn) {
MainPage = new NavigationPage (new LoginPage ());
} else {
MainPage = new NavigationPage (new LoginNavigation.MainPage ());
}
}
protected override void OnStart ()
{
// Handle when your app starts
}
protected override void OnSleep ()
{
// Handle when your app sleeps
}
protected override void OnResume ()
{
// Handle when your app resumes
}
}
}
- 다시 C# 프로젝트에서 마우스 오른쪽 버튼을 클릭하여 클래스 파일을 추가 합니다
- 클래스 명은 Constants.cs로 만들고 추가 합니다
- 새로 추가된 클래스 파일에 아래와 같이 코딩합니다
*Constants.cs
using System;
using System.Data;
namespace LoginNavigation
{
public static class Constants
{
public static string Username = string.Empty;
public static string Password = string.Empty;
}
}
using System;
using System.Linq;
using Xamarin.Forms;
namespace LoginNavigation
{
public partial class SignUpPage : ContentPage
{
public SignUpPage ()
{
InitializeComponent ();
}
async void OnSignUpButtonClicked (object sender, EventArgs e)
{
var user = new User () {
Username = usernameEntry.Text,
Password = passwordEntry.Text,
Email = emailEntry.Text
};
// Sign up logic goes here
var signUpSucceeded = AreDetailsValid (user);
if (signUpSucceeded) {
var rootPage = Navigation.NavigationStack.FirstOrDefault ();
if (rootPage != null) {
App.IsUserLoggedIn = true;
Navigation.InsertPageBefore (new MainPage (), Navigation.NavigationStack.First ());
await Navigation.PopToRootAsync ();
}
} else {
messageLabel.Text = "Sign up failed";
}
}
bool AreDetailsValid (User user)
{
return (!string.IsNullOrWhiteSpace (user.Username) && !string.IsNullOrWhiteSpace (user.Password) && !string.IsNullOrWhiteSpace (user.Email) && user.Email.Contains ("@"));
}
}
}
- 다시 클래스 파일을 추가 합니다
- 추가될 클래스 파일의 명칭은 SingUppageCS.cs 입니다
- 추가된 클래스 파일에 아래와 같이 코딩 합니다
*SignUpPage.xaml.cs
using System;
using System.Linq;
using Xamarin.Forms;
namespace LoginNavigation
{
public class SignUpPageCS : ContentPage
{
Entry usernameEntry, passwordEntry, emailEntry;
Label messageLabel;
public SignUpPageCS ()
{
messageLabel = new Label ();
usernameEntry = new Entry {
Placeholder = "username"
};
passwordEntry = new Entry {
IsPassword = true
};
emailEntry = new Entry ();
var signUpButton = new Button {
Text = "Sign Up"
};
signUpButton.Clicked += OnSignUpButtonClicked;
Title = "Sign Up";
Content = new StackLayout {
VerticalOptions = LayoutOptions.StartAndExpand,
Children = {
new Label { Text = "Username" },
usernameEntry,
new Label { Text = "Password" },
passwordEntry,
new Label { Text = "Email address" },
emailEntry,
signUpButton,
messageLabel
}
};
}
async void OnSignUpButtonClicked (object sender, EventArgs e)
{
var user = new User () {
Username = usernameEntry.Text,
Password = passwordEntry.Text,
Email = emailEntry.Text
};
// Sign up logic goes here
var signUpSucceeded = AreDetailsValid (user);
if (signUpSucceeded) {
var rootPage = Navigation.NavigationStack.FirstOrDefault ();
if (rootPage != null) {
App.IsUserLoggedIn = true;
Navigation.InsertPageBefore (new MainPageCS (), Navigation.NavigationStack.First ());
await Navigation.PopToRootAsync ();
}
} else {
messageLabel.Text = "Sign up failed";
}
}
bool AreDetailsValid (User user)
{
return (!string.IsNullOrWhiteSpace (user.Username) && !string.IsNullOrWhiteSpace (user.Password) && !string.IsNullOrWhiteSpace (user.Email) && user.Email.Contains ("@"));
}
}
}
- 다시 클래스 파일을 하나더 추가 합니다
- 클래스 명칭은 User.cs 입니다
- 추가된 User.cs 파일에 아래와 같이 코딩 합니다
*User.cs
namespace LoginNavigation
{
public class User
{
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
}