.NET 5 開發WPF - 美食應用登陸UI設計

Demo演示:
git

你的時間寶貴,不想看囉嗦的文字,可直接拉到文末下載源碼!github

1. 新建項目

站長開發環境:服務器

  • VS 2019企業版 16.70
  • .NET 5 Preview 5

.NET 5 WPF 項目模板和 .NET Core 3.1 WPF 項目模板沒有區別,建立好項目後,NuGet 引入 MaterialDesignThemes 庫:app

安裝MaterialDesignThemes庫

2.引入樣式

演示Demo就一個xaml文件和xaml.cs文件,爲了方便後面收集WPF界面設計效果,統一放在了開源項目TerminalMACS.ManagerForWPF中,因此控件樣式引用直接在FoodAppLoginView.xaml中添加:dom

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Amber.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

3.控件動畫效果

見上面GIF動畫,登陸窗口加載時,用戶名輸入框、密碼輸入框、記住密碼、右側背景圖片等元素有動畫效果,每一個部分代碼結構相似,好比下面的用戶名輸入框代碼:async

<!--#region User name textblox-->
<materialDesign:TransitioningContent Grid.Row="2" Margin="90,20,00,0" HorizontalAlignment="Left">
    <materialDesign:TransitioningContent.OpeningEffects>
        <materialDesign:TransitionEffect Kind="SlideInFromLeft" Duration="0:0:2"/>
    </materialDesign:TransitioningContent.OpeningEffects>
    <StackPanel Style="{StaticResource setVisibilityBasedLogin}" Orientation="Horizontal">
        <materialDesign:PackIcon Kind="Account" Width="16" Height="16" VerticalAlignment="Center"
                         Margin="0,5,10,0" Foreground="{Binding ElementName=NameTextBox, Path=BorderBrush}"/>
        <TextBox x:Name="NameTextBox" Width="140" materialDesign:HintAssist.Hint="{markup:I18n {x:Static i18NResources:Language.FoodAppLoginView_UserName}}"
                 Style="{StaticResource MaterialDesignFloatingHintTextBox}"/>
    </StackPanel>
</materialDesign:TransitioningContent>
<!--#endregion-->

使用了開源控件MD的TransitioningContent組件,其中TransitionEffect的Kind屬性設置控件動畫方向。ide

4. 模擬登陸

登陸按鈕佈局代碼:佈局

<!--#region control panel-->
<materialDesign:TransitioningContent Grid.Row="4" Margin="40,20,0,0">
    <materialDesign:TransitioningContent.OpeningEffects>
        <materialDesign:TransitionEffect Kind="SlideInFromBottom" Duration="0:0:2"/>
    </materialDesign:TransitioningContent.OpeningEffects>
    <StackPanel Style="{StaticResource setVisibilityBasedLogin}" Orientation="Horizontal" HorizontalAlignment="Center">
        <CheckBox Content="{markup:I18n {x:Static i18NResources:Language.FoodAppLoginView_RememberMe}}"/>
        <Button Style="{StaticResource MaterialDesignRaisedButton}"
            Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
            materialDesign:ButtonAssist.CornerRadius="20"
            Width="80" Height="40" Margin="120,0,0,0"
                Content="{markup:I18n {x:Static i18NResources:Language.FoodAppLoginView_Login}}"/>
    </StackPanel>
</materialDesign:TransitioningContent>
<!--#endregion-->

點擊登陸時,打開等待對話框(點擊時綁定了materialDesign:DialogHost.OpenDialogCommand),在等待對話框的打開與關閉事件中作登陸邏輯處理。動畫

private async Task<bool> ValidateCreds()
{
    // 模擬登陸
    // 你能夠發送登陸信息到服務器,獲得認證回饋
    await Task.Delay(TimeSpan.FromSeconds(2));
    Random gen = new Random(DateTime.Now.Millisecond);
    int loginProb = gen.Next(100);
    return loginProb <= 20;
}

private async void OpenCB_DialogOpened(object sender, MaterialDesignThemes.Wpf.DialogOpenedEventArgs eventArgs)
{
    try
    {
        this.IsJustStarted = true;
        this.LoginStatusmsg = "";
        bool isLoggedIn = await ValidateCreds();
        if (isLoggedIn)
        {
            // 須要關閉登陸對話框並顯示主窗口
            eventArgs.Session.Close(true);
        }
        else
        {
            // 登陸失敗,設置false做爲參數
            eventArgs.Session.Close(false);
        }
    }
    catch (Exception)
    {

        //throw;
    }
}

private void ClosingCB_DialogClosing(object sender, MaterialDesignThemes.Wpf.DialogClosingEventArgs eventArgs)
{
    if (eventArgs.Parameter == null)
    {
        return;
    }
    IsLoggedIn = (bool)eventArgs.Parameter;
    IsJustStarted = false;
    if(IsLoggedIn)
    {
        this.LoginStatusmsg = I18nManager.Instance.Get(I18nResources.Language.FoodAppLoginView_Success).ToString();
    }
    else
    {
        this.LoginStatusmsg = I18nManager.Instance.Get(I18nResources.Language.FoodAppLoginView_Fail).ToString();
    }
}

等待對話框打開事件中,模擬登陸邏輯。this

等待對話框關閉事件中,作界面響應信息。

5. 源碼下載

上面只貼了部分關鍵代碼,源碼已放Github中。

參考視頻:WPF Food App Login UI Material Design [Speed Design]

Demo源碼:FoodAppLoginUI

相關文章
相關標籤/搜索