首頁>技術>

.NET CORE(C#) WPF 重新設計Instagram

閱讀導航

本文背景程式碼實現本文參考原始碼1. 本文背景

老外的一個介面設計,站長覺得不錯,分享給大家作為參考,難度不大,主要是介面佈局設計。

2. 程式碼實現

使用 .NET CORE 3.1 建立名為 “InstagramRedesign” 的WPF模板專案,新增1個Nuget庫:MaterialDesignThemes,版本為最新預覽版3.1.0-ci948。

解決方案主要檔案目錄組織結構:

InstagramRedesignAssets數張圖片,從Github上下載App.xamlMainWindow.xamlMainWindow.xaml.cs2.1 引入樣式

檔案【App.xaml】,在 StartupUri 中設定啟動的檢視【MainWindow.xaml】,並在【Application.Resources】節點增加 MaterialDesignThemes庫的樣式檔案:

<Application.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Application.Resources>
2.2 演示窗體佈局

檔案【MainWindow.xaml】,佈局程式碼,原始碼如下:

<Window x:Class="InstagramRedesign.MainWindow"        xmlns="/file/2020/01/22/20200122015354_469.jpg.aspx        xmlns:materialDesign="/file/2020/01/22/20200122015355_470.jpg        xmlns:d="/file/2020/01/22/20200122015356_471.jpg.aspx        xmlns:x="/file/2020/01/22/20200122015357_472.jpg.aspx        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" MouseLeftButtonDown="MoveWindow_MouseLeftButtonDown"        mc:Ignorable="d" Height="600" Width="1080" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">    <Grid Background="#FF44325F">        <Grid Height="100" VerticalAlignment="Top" Background="White">            <Image Source="Assets/insta.jpg" HorizontalAlignment="Left" Margin="10 0"/>            <TextBlock Text="Sunshine mixed with a little Hurricane✨" Margin="10" FontSize="14" Foreground="#FF2A6088" HorizontalAlignment="Center"/>            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">                <StackPanel VerticalAlignment="Bottom">                    <TextBlock Text="追隨者" Margin="10 0" FontSize="16" FontWeight="Bold" Foreground="#FF2A6088" />                    <TextBlock Text="2K" HorizontalAlignment="Center" Margin="5 0 5 5" FontSize="16" Foreground="#FF2A6088"/>                </StackPanel>                <StackPanel VerticalAlignment="Bottom">                    <TextBlock Text="跟隨" Margin="10 0" FontSize="16" FontWeight="Bold" Foreground="#FF2A6088"/>                    <TextBlock Text="957" HorizontalAlignment="Center" Margin="5 0 5 5" FontSize="16" Foreground="#FF2A6088"/>                </StackPanel>                <StackPanel VerticalAlignment="Bottom">                    <TextBlock Text="帖子" Margin="10 0" FontSize="16" FontWeight="Bold" Foreground="#FF2A6088"/>                    <TextBlock Text="145" HorizontalAlignment="Center" Margin="5 0 5 5" FontSize="16" Foreground="#FF2A6088"/>                </StackPanel>            </StackPanel>        </Grid>        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="20" >            <TextBlock FontFamily="Nirmala UI" Text="Dotnet9.com" FontSize="30" Margin="20" VerticalAlignment="Center" Foreground="White"/>            <StackPanel HorizontalAlignment="Right">                <Ellipse Height="150" Width="150" Stroke="White" StrokeThickness="3">                    <Ellipse.Fill>                        <ImageBrush ImageSource="/file/2020/01/22/20200122015358_473.jpg.png" Stretch="UniformToFill"/>                    </Ellipse.Fill>                </Ellipse>                <Button HorizontalAlignment="Center" Margin="10" Content="編輯個人資訊" Background="{x:Null}" BorderBrush="White"/>            </StackPanel>        </StackPanel>        <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="10 110">            <TextBox Width="150" materialDesign:HintAssist.Hint="搜尋" Foreground="White"/>            <materialDesign:PackIcon Kind="AccountSearch" Foreground="White" VerticalAlignment="Center"/>        </StackPanel>        <ListView HorizontalAlignment="Right" Margin="0 220 0 0">            <ListViewItem Height="50" Width="260">                <Grid Height="30" Width="250">                    <Ellipse Width="30" Height="30" StrokeThickness="0.5" Stroke="White" HorizontalAlignment="Left">                        <Ellipse.Fill>                            <ImageBrush ImageSource="Assets/pexels-photo-192439.jpeg" Stretch="UniformToFill"/>                        </Ellipse.Fill>                    </Ellipse>                    <TextBlock Width="150" HorizontalAlignment="Left"  Margin="35 0" Text="Your friend John is on instagram as John_J" FontSize="10" TextWrapping="Wrap"/>                    <Button HorizontalAlignment="Right" Content="跟隨" FontSize="10" Margin="10 0" Height="20" Padding="10 0"/>                </Grid>            </ListViewItem>            <ListViewItem Height="50" Width="260">                <Grid Height="30" Width="250">                    <Ellipse Width="30" Height="30" StrokeThickness="0.5" Stroke="White" HorizontalAlignment="Left">                        <Ellipse.Fill>                            <ImageBrush ImageSource="Assets/pexels-photo-301290.jpeg" Stretch="UniformToFill"/>                        </Ellipse.Fill>                    </Ellipse>                    <TextBlock Width="150" HorizontalAlignment="Left"  Margin="35 0" Text="Your friend Lucy is on instagram as LLucy" FontSize="10" TextWrapping="Wrap"/>                    <Button HorizontalAlignment="Right" Content="跟隨" FontSize="10" Margin="10 0" Height="20" Padding="10 0"/>                </Grid>            </ListViewItem>            <ListViewItem Height="50" Width="260">                <Grid Height="30" Width="250">                    <Ellipse Width="30" Height="30" StrokeThickness="0.5" Stroke="White" HorizontalAlignment="Left">                        <Ellipse.Fill>                            <ImageBrush ImageSource="Assets/pexels-photo-614810.jpeg" Stretch="UniformToFill"/>                        </Ellipse.Fill>                    </Ellipse>                    <TextBlock Width="150" HorizontalAlignment="Left"  Margin="35 0" Text="Your friend _b_b_jax is on instagram as bbjax" FontSize="10" TextWrapping="Wrap"/>                    <Button HorizontalAlignment="Right" Content="跟隨" FontSize="10" Margin="10 0" Height="20" Padding="10 0"/>                </Grid>            </ListViewItem>            <ListViewItem Height="50" Width="260">                <Grid Height="30" Width="250">                    <Ellipse Width="30" Height="30" StrokeThickness="0.5" Stroke="White" HorizontalAlignment="Left">                        <Ellipse.Fill>                            <ImageBrush ImageSource="Assets/pexels-photo-638791.jpeg" Stretch="UniformToFill"/>                        </Ellipse.Fill>                    </Ellipse>                    <TextBlock Width="150" HorizontalAlignment="Left"  Margin="35 0" Text="Your friend Carl is on instagram as CJ" FontSize="10" TextWrapping="Wrap"/>                    <Button HorizontalAlignment="Right" Content="跟隨" FontSize="10" Margin="10 0" Height="20" Padding="10 0"/>                </Grid>            </ListViewItem>        </ListView>        <StackPanel Margin="0 150 300 0">            <StackPanel Orientation="Horizontal" Margin="10">                <Ellipse Height="70" Width="70" Margin="10" Stroke="White" StrokeThickness="3">                    <Ellipse.Fill>                        <ImageBrush ImageSource="Assets/pexels-photo-192439.jpeg" Stretch="UniformToFill"/>                    </Ellipse.Fill>                </Ellipse>                <Ellipse Height="70" Width="70" Margin="10" Stroke="White" StrokeThickness="3">                    <Ellipse.Fill>                        <ImageBrush ImageSource="Assets/pexels-photo-301290.jpeg" Stretch="UniformToFill"/>                    </Ellipse.Fill>                </Ellipse>                <Ellipse Height="70" Width="70" Margin="10" Stroke="White" StrokeThickness="3">                    <Ellipse.Fill>                        <ImageBrush ImageSource="Assets/pexels-photo-614810.jpeg" Stretch="UniformToFill"/>                    </Ellipse.Fill>                </Ellipse>                <Ellipse Height="70" Width="70" Margin="10" Stroke="White" StrokeThickness="3">                    <Ellipse.Fill>                        <ImageBrush ImageSource="Assets/pexels-photo-638791.jpeg" Stretch="UniformToFill"/>                    </Ellipse.Fill>                </Ellipse>            </StackPanel>            <ScrollViewer Height="340">                <StackPanel>                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">                        <Image Width="200" Height="200" Margin="20" Source="Assets/pexels-photo-257360.jpeg" Stretch="UniformToFill"/>                        <Image Width="200" Height="200" Margin="20" Source="Assets/pexels-photo-288583.jpeg" Stretch="UniformToFill"/>                        <Image Width="200" Height="200" Margin="20" Source="Assets/pexels-photo-790164.jpeg" Stretch="UniformToFill"/>                    </StackPanel>                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">                        <Image Width="200" Height="200" Margin="20" Source="Assets/food-salad-restaurant-person.jpg" Stretch="UniformToFill"/>                        <Image Width="200" Height="200" Margin="20" Source="Assets/pexels-photo-797640.jpeg" Stretch="UniformToFill"/>                        <Image Width="200" Height="200" Margin="20" Source="Assets/pexels-photo-790164.jpeg" Stretch="UniformToFill"/>                    </StackPanel>                </StackPanel>            </ScrollViewer>        </StackPanel>    </Grid></Window>

檔案【MainWindow.xaml.cs】,後臺站長自己加了一個窗體移動操作:

private void MoveWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){    DragMove();}
3.本文參考視訊一:C# WPF Material Design UI: Redesign Instagram,配套原始碼:Instagram。C# WPF開源控制元件庫《MaterialDesignInXAML》4.原始碼

效果圖實現程式碼在文中已經全部給出,從配套原始碼中拷貝圖片,按解決方案目錄組織程式碼檔案即可執行。


時間如流水,只能流去不流回!

此刻順便為我點個《【再看】》可好?

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 「3.0」一個人開發一個App,小程式從0到1,刪減新增