• 章节:26
  • 访问:672
  • 时长:00:16:49
  • 日期:2020-01-10 08:55

增加一个用来管理获取模板的页面

clipboard2.pngclipboard2.png

<Page x:Class="WeixinManager.Template"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WeixinManager"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Template">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" VerticalAlignment="Center" Orientation="Horizontal">
            <Button Content="刷新" Width="120" Margin="5" HorizontalAlignment="Left" Height="30" Click="Button_Click"/>
            <Label Name="tbInfo" VerticalAlignment="Center" Foreground="Red"></Label>
        </StackPanel>
        <DataGrid Grid.Row="1" Name="dgTemplate" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding title}" Header="标题"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding primary_industry}" Header="一级行业"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding deputy_industry}" Header="二级行业"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding content}" Header="内容"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding example}" Header="例子"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding template_id}" Header="模板ID"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Page>
public partial class Template : Page
    {
        public Template()
        {
            InitializeComponent();
            InitDataGrid();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var appid = ConfigurationManager.AppSettings["appID"];

            using (var db = SugarDao.GetInstance())
            {
                var templateList = TemplateHelper.LoadTemplate();
                var res = true;
                db.BeginTran();
                db.Delete<Model.Template>(m => m.appid == appid);
                foreach (var item in templateList.template_list)
                {
                    item.appid = appid;
                    res = res && Convert.ToBoolean(db.Insert(item));
                }
                if (res)
                {
                    db.CommitTran();
                }
                else
                {
                    db.RollbackTran();
                }
                InitDataGrid();
            }
            MessageBox.Show("刷新成功");
        }

        private void InitDataGrid()
        {
            var appid = ConfigurationManager.AppSettings["appID"];
            using (var db = SugarDao.GetInstance())
            {
                List<Model.Template> list = db.Queryable<Model.Template>().Where(m => m.appid == appid.ObjToString()).ToList();//查询所有
                this.dgTemplate.ItemsSource = list;
            }
        }
    }

业务逻辑 就是每次先把当前公众号(appid区分)是模板记录删除 然后再新增