首页 > 其他分享 >wpf小说阅读器 ----wpf练习demo

wpf小说阅读器 ----wpf练习demo

时间:2023-07-05 15:12:12浏览次数:42  
标签:username string demo System ---- using wpf password public

1.登录窗口布局

  <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        <Border >
            <Border.Background>
                <ImageBrush ImageSource="/Wemail.Resource;component/imgs/1.png"/>

            </Border.Background>
            <Grid >

                <Grid.RowDefinitions>
                    <RowDefinition Height="120"/>
                    <RowDefinition Height="80"/>
                    <RowDefinition  Height="50"/>
                    <RowDefinition  Height="80"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="1" Text="注册" FontSize="30" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Text="注册一个新账号" FontSize="18" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <Grid Grid.Row="3" Width="140" Height="60">
                    <Button  materialDesign:ButtonAssist.CornerRadius="25"
                
                   Style="{StaticResource MaterialDesignRaisedButton}"
                   ToolTip="点击注册">
                        <TextBlock Text="注册" />
                    </Button>
                </Grid>

            </Grid>
        </Border>

        <Grid Grid.Column="1" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="211*"/>
                <ColumnDefinition Width="56*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="160"/>
                <RowDefinition Height="10"/>
                <RowDefinition Height=" 50"/>
                <RowDefinition Height="50"/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Text="用户登录"  FontSize="50" Foreground="BurlyWood" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="167,0,0,0"/>
            <Border BorderBrush="Gray" BorderThickness="1" Grid.Row="2" Margin="150,0,39,0">
                <Grid Width="210" Height="30" >
                    <materialDesign:PackIcon Kind="EmailCheckOutline" Foreground="Gray" Height="30" Width="30" Margin="0 5 0 0"/>
                    <TextBox  x:Name="txtEmail"   Width="170" Height="35" HorizontalAlignment="Right" materialDesign:HintAssist.Hint="请输入邮箱号" 
                              VerticalContentAlignment="Bottom" FontSize="15" Text="{Binding  Username, UpdateSourceTrigger=PropertyChanged}"  ></TextBox>
                </Grid>

            </Border>
            <Border BorderBrush="Gray" BorderThickness="1" Grid.Row="3" Margin="150,0,39,0">
                <Grid Width="210" Height="35" >
                    <materialDesign:PackIcon Kind="LockOutline" Foreground="Gray" Height="30" Width="30" Margin="0 5 0 0"/>
                    <PasswordBox  x:Name="txtPassword"  Width="170" Height="35" HorizontalAlignment="Right" materialDesign:HintAssist.Hint="请输入密码" 
                                  VerticalContentAlignment="Bottom" FontSize="15" Cursor="IBeam" 
                                  base:PasswordBoxHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  base:PasswordBoxHelper.Attach="true"></PasswordBox>
                </Grid>

            </Border>
            <Grid Grid.Row="4" Margin="197,0,85,0">
                <Button  BorderThickness="0" x:Name="LoginBtn" Background="#39aaa8" Style="{StaticResource MaterialDesignRaisedDarkButton}" Height="40" 
                         materialDesign:ButtonAssist.CornerRadius="20" Command="{Binding LoginCommand }"
                         CommandParameter="{Binding .,RelativeSource={RelativeSource AncestorType=Window}}">
                    <TextBlock Foreground="LightGray" Text="登  录" />
                </Button>
            </Grid>
            <Grid Grid.Row="5" Margin="197,0,85,0">
                <Button  BorderThickness="0"  Background="#39aaa8" Style="{StaticResource MaterialDesignRaisedDarkButton}" Height="40"  x:Name="click" Click="LoginBtn_Click"
                         materialDesign:ButtonAssist.CornerRadius="20" 
                       >
                    <TextBlock Foreground="LightGray" Text="游客登录" />
                </Button>
            </Grid>
        </Grid>

    </Grid>

2.Mainwindow 布局

 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80"></RowDefinition>
            <RowDefinition ></RowDefinition>
        </Grid.RowDefinitions>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="270"/>
                <ColumnDefinition Width="1"/>
                <ColumnDefinition Width="320"/>
                <ColumnDefinition Width="1"/>
                <ColumnDefinition Width="8*"/>
            </Grid.ColumnDefinitions>
            <ListView ItemsSource="{Binding DataCollection}" x:Name="listView" Style="{StaticResource NoScrollBarListViewStyle}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <materialDesign:Card Width="250" >
                            <Grid >
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="140" />
                                    <RowDefinition  Height="*" />
                                    <RowDefinition  Height="Auto" />
                                </Grid.RowDefinitions>
                                <Image  Height="140"  Source="{Binding cover }"  />
                                <StackPanel Orientation="Horizontal" Grid.Row="1" >
                                    <TextBlock Text="{Binding title}" FontSize="15"/>
                                    <TextBlock Text="  " FontSize="15"/>
                                    <TextBlock Text="作者:" FontSize="15"/>
                                    <TextBlock Text="{Binding author}" FontSize="15"/>
                                </StackPanel>
                                <StackPanel Grid.Row="1" Margin="8,24,8,0">
                                    <TextBlock   VerticalAlignment="Center" Text="{Binding descs}"
                                       TextWrapping="Wrap" />
                                    <Button  materialDesign:ButtonProgressAssist.IsIndeterminate="True"
                                        materialDesign:ButtonProgressAssist.IsIndicatorVisible="True"
                                        materialDesign:ButtonProgressAssist.Value="-1" Content="开始阅读" 
                                        Command="{Binding  command}" CommandParameter="{Binding fictionId}"
                                        Style="{StaticResource MaterialDesignRaisedButton}" />
                                </StackPanel>
                            </Grid>
                        </materialDesign:Card>
                    </DataTemplate>
                    
                </ListView.ItemTemplate>
            </ListView>
            <Rectangle Grid.Column="1" Width="1" Fill="Black"></Rectangle>
            <Rectangle Grid.Column="3" Width="1" Fill="Black"></Rectangle>
            <ListView ItemsSource="{Binding ChapterList}"   Grid.Column="2" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding title}" FontSize="15"/>
                            <Button Margin="2,0" materialDesign:ButtonProgressAssist.IsIndeterminate="True"
                                        materialDesign:ButtonProgressAssist.IsIndicatorVisible="True"
                                        materialDesign:ButtonProgressAssist.Value="-1" Content="开始阅读" 
                                        Height="25"          Command="{Binding cmd}" CommandParameter="{Binding chapterId}" 
                                        Style="{StaticResource MaterialDesignRaisedButton}" />
                        </StackPanel>
                            
                            
                        
                      
                    </DataTemplate>

                </ListView.ItemTemplate>
            </ListView>
            <Grid Grid.Column="4" >

                <TextBox Text="{Binding Content}" AcceptsReturn="True" TextWrapping="Wrap" Margin="20,20,20,0" FontSize="20"/>
            </Grid>
        </Grid>
        <materialDesign:ColorZone Padding="16" Mode="PrimaryMid">
            <DockPanel>
                <StackPanel  materialDesign:RippleAssist.IsCentered="True"  Orientation="Horizontal">
                    <materialDesign:ColorZone Margin="16,0,0,0"  Padding="8,4,8,4"  Panel.ZIndex="1"   materialDesign:ElevationAssist.Elevation="Dp2"
                     CornerRadius="2"  Mode="Standard">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition   Width="Auto" />
                                <ColumnDefinition  Width="*" />
                                <ColumnDefinition     Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Button Style="{StaticResource MaterialDesignToolButton}" Command="{Binding SearchCommand}">
                                <materialDesign:PackIcon   Kind="Search" Opacity=".56" />
                            </Button>
                  
                            <TextBox     Grid.Column="1"   MinWidth="200"  Margin="8,0,0,0" VerticalAlignment="Center"
            materialDesign:HintAssist.Hint="Build a search bar"
            materialDesign:TextFieldAssist.DecorationVisibility="Hidden" Text="{Binding SearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
            BorderThickness="0" />
                        </Grid>
                    </materialDesign:ColorZone>

                </StackPanel>
            </DockPanel>
        </materialDesign:ColorZone>

    </Grid>

3.数据库配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="demoDB" connectionString="Data Source=localhost;Database=wemail;User ID=sa;Password=123456;TrustServerCertificate=true"/>
    </connectionStrings>
</configuration>

4.登录逻辑

using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wemail.DAL
{
    public class SqlServerAccess

    {    
      //读取app.config中数据库连接字符串
        string connectionString = ConfigurationManager.ConnectionStrings["demoDB"].ConnectionString;
        public bool IsValid(string username, string password)
        {   //读取app.config中数据库连接字符串
           
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                // 查询数据库获取对应用户记录
                string query = "SELECT COUNT(*) FROM [user] WHERE username = @username AND password = @password";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@username", username);
                    command.Parameters.AddWithValue("@password", password);

                    int count = (int)command.ExecuteScalar();

                    if (count > 0)
                    {
                        return true;  // 用户名和密码有效
                    }
                    else
                    {
                        return false;  // 用户名和密码无效
                    }
                }
            }
        }
    }
}

5.在LoginwindowViewModel里面完成登录逻辑调用

using Microsoft.Data.SqlClient;
using Microsoft.IdentityModel.Tokens;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;
using Wemail.DAL;
using Wemail.Views;

namespace Wemail.ViewModels
{
    public class LoginWindowViewModel : BindableBase
    {
        private string _username="admin";
        private string _password="123456";
        public string Username { get { return _username; } set { SetProperty(ref _username, value); } }
        public string Password { get { return _password;}set { SetProperty(ref _password, value);} }
        public ICommand LoginCommand { get => new DelegateCommand<object>(Login);  }

        SqlServerAccess sqlServerAccess = new SqlServerAccess();
        LoginWindow loginWindow = new LoginWindow();
        public LoginWindowViewModel()
        {

        }
        private void Login(object obj)
        {     
            //进行登录校验
            if (sqlServerAccess.IsValid(Username, Password))
            {
               (obj as Window).DialogResult=true;
            }
            else MessageBox.Show("账号或密码错误!");
        }

    }
}

 

 

完整代码   https://gitee.com/zhaosanmao/wpf-exercise-demo

运行截图:

 

标签:username,string,demo,System,----,using,wpf,password,public
From: https://www.cnblogs.com/zhaosanmao/p/17528598.html

相关文章

  • 配置 containerd 镜像仓库完全攻略
    前言 Kubernetes在Changelog中宣布自Kubernetes1.20之后将弃用Docker作为容器运行时之后,containerd成为下一个容器运行时的热门选项。虽然containerd很早就已经是Docker的一部分,但是纯粹使用containerd还是给大家带来了诸多困扰,本文将介绍如何使用containerd......
  • 建筑师们,你们该把模型弄到PPT里做汇报了!
    ➤如何实现项目汇报效率比传统的PPT高,同时汇报效果更直观?➤如何摆脱方案汇报只能向客户交付数据,安装专业软件查看项目成果?➤如何将无人机航测数据、CAD图纸、BIM设计成果进行融合,挖掘出更深层次的应用?➤如何创建一个高大上的实景三维展示系统向甲方进行工作汇报?     ......
  • Django REST framework drf 后端分页
    使用view.pyfromrest_frameworkimportpaginationclassdefault_pagination(pagination.PageNumberPagination):page_size=15#分页的条数page_query_param='page'page_size_query_param='size' classrun_sh(generics.ListCreate......
  • 11.UG移动面(高级教程)
    1.移动面-通过点与点之间的距离移动1.1选择原点、测量点、方向 2.输入距离即可将面移动到距离该测量点一定距离位置......
  • hadoop集群配置 CentOS7 JDK安装 卸载
    1JDK下载下载JDK8下载地址 2SSH访问需要安装JDK的服务器3检查是否有默认安装的JDKjava-version 4卸载自带JDK4.1检查系统安装的OpenJDKrpm-qa|grepopenjdk-i 4.2  删除以上四项openjdk的安装包并且检查后显示删除完毕rpm-e--nodeps需要删除的软......
  • MySQL数据库8.0.29-8.0.31版本使用 INSTANT 算法新增字段bug
    xxx下发MySQL数据库共性隐患排查通知,要求统一排查MySQL数据库8.0.29及以后版本使用INSTANT算法新增字段后期变更回滚可能导致数据库宕机的隐患,排查方法及整改方法详见下表和附件。请各分支()数据库运营人员集中排查隐患,及时整改。 隐患概述MySQL数据库8.0.29及以后版本......
  • cfssl 自签证书
    cfssl1**.1准备cfssl证书生成工具**cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便使用。找任意一台服务器操作,这里用Master节点。wgethttps://pkg.cfssl.org/R1.2/cfssl_linux-amd64wgethttps://pkg.cfssl.org/R1.2/cfssljson_linux-amd64wgetht......
  • 企业级logstash简单使用(ELK)
    企业级logstash简单使用(ELK)要使用logstash收集到Elasticsearch的方式,需确保logstash版本与es版本一致。由于我也是刚刚研究使用,所以本文暂不会出现原理性的东西。Logstash介绍Logstash是具有实时流水线能力的开源的数据收集引擎。Logstash可以动态统一不同来源的数据,并将数......
  • 对企业架构中的业务架构关键点的进一步说明
     因为最近在重新规整企业架构方面的资料和文章,特别是企业架构中的业务架构部分,因此今天想重点对业务架构的一些关键点进行说明。注:这篇文章仅仅作为关键点的记录,不作为体系化介绍业务架构的文章。如果希望了解完整的企业架构和业务架构情况参考EA和TOGAF相关资料。完整的......
  • 02-贷前审批策略搭建思路
    2.贷前审批策略搭建思路贷前审批策略是金融风控里重要的一环,是整个贷款流程的基础,应用于筛选目标服务客群。高质量的贷前审批策略搭建是防范风险、减少坏账的重要前提。如何搭建贷前策略审批架构?首先考虑条件:(1)显性条件:行业普遍存在的一些条件(2)隐形条件:与企业自身特定的实际业......