用户控件中的C#WPF应用程序保存设置

时间:2017-02-24 07:55:31

标签: c# wpf user-controls

我有一个主要的应用程序,这里有边框不同的按钮。这还包括" Einstellung"按钮。按此按钮应显示与以前不同的内容。我用UserControl实现了这一点。

现在您可以在此用户控件中进行设置并保存它们。

enter image description here

我在设置中进行了这项保存

并使用代码

<TabControl HorizontalAlignment="Stretch" VerticalContentAlignment="Stretch" x:Name="tabControl">
    <TabControl.Resources>
        <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFF" Offset="0.0"/>
                    <GradientStop Color="#EEE" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
        <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
        <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
        <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
        <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
        <ControlTemplate x:Key="simpleTI" TargetType="TabItem">
            <Grid>
                <Border Name="Border" 
                            BorderThickness="0,0,0,2" 
                            Background="{StaticResource LightBrush}"
                            BorderBrush="{StaticResource LightBrush}" 
                            CornerRadius="0" >
                    <ContentPresenter x:Name="ContentSite" Margin="5,0"  VerticalAlignment="Center" HorizontalAlignment="Center"
                                          ContentSource="Header"  RecognizesAccessKey="True"/>
                </Border>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter TargetName="Border" Property="BorderBrush" Value="#57B481" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                    <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </TabControl.Resources>


    <TabItem  MaxWidth="55" HorizontalAlignment="Center" Background="White" Template="{StaticResource simpleTI}" >
        <TabItem.Header>
            <TextBlock TextAlignment="Center" Text="Actions Needed" TextWrapping="Wrap" />
        </TabItem.Header>
    </TabItem>
    <TabItem  MaxWidth="55" HorizontalAlignment="Center" Background="White" Template="{StaticResource simpleTI}" >
        <TabItem.Header>
            <TextBlock TextAlignment="Center" Text="Actions Needed 1" TextWrapping="Wrap" />
        </TabItem.Header>
    </TabItem>
    <TabItem  MaxWidth="55" HorizontalAlignment="Center" Background="White" Template="{StaticResource simpleTI}" >
        <TabItem.Header>
            <TextBlock TextAlignment="Center" Text="Actions Needed 2" TextWrapping="Wrap" />
        </TabItem.Header>
    </TabItem>
</TabControl>

现在我按下&#34; Speichern&#34;按钮。他救了我的设置。 我现在更改UserControl,所以我点击按钮&#34; Vorschau&#34;并向我显示一个新内容。如果我现在再次点击&#34; Einstellung&#34;是存在的内容,因为我已经存储了它。

但问题来了。我关闭了应用程序,然后再打开它。现在我点击按钮&#34; Einstellung&#34;在文本框中只有0位于其中。

enter image description here

不应该这样,但内容应该来自那里。

启动应用程序时检索此内容。请参阅代码段。

Vorschau.Properties.Settings.Default.BreiteBitmap = Int32.Parse(tbBreiteBitmap.Text);
Vorschau.Properties.Settings.Default.HoeheBitmap = Int32.Parse(tbHoeheBitmap.Text);
Vorschau.Properties.Settings.Default.AnzahlKoordinaten = Int32.Parse(tbAnzahlKoordinaten.Text);

AND

 public UCEinstellung()
        {
            InitializeComponent();

            //Lade der Einstellungen
            tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
            tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
            tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
        }

不幸的是,这不起作用。 我希望你能告诉我错误在哪里。在另一个没有usercontrol工作的应用程序中,这个存储并正确检索。

UCEinstellung

public MainWindow()
        {
            InitializeComponent();
            MouseDown += Window_MouseDown;

            einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
            einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
            einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();

        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Vorschau
{
    /// <summary>
    /// Interaktionslogik für UCEinstellung.xaml
    /// </summary>
    public partial class UCEinstellung : UserControl
    {
        public UCEinstellung()
        {
            InitializeComponent();

            //Lade der Einstellungen
            tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
            tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
            tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();
        }

        private static Boolean kontrolliereObZahl(String pText)
        {
            int zahl1;
            double zahl2;
            Boolean istZahl;
            if (Int32.TryParse(pText, out zahl1) || Double.TryParse(pText, out zahl2))
            {
                istZahl = true;
            }
            else
            {
                istZahl = false;
            }
            return istZahl;
        }

        private static Boolean textfeldRot(String pBreiteBitmap, String pHoeheBitmap, String pAnzahlKoordinaten)
        {
            Boolean istRot = true;
            if (pBreiteBitmap.Equals("61381638") || pHoeheBitmap.Equals("61381638") || pAnzahlKoordinaten.Equals("61381638"))
            {
                istRot = true;
            }
            else
            {
                istRot = false;
            }
            return istRot;
        }
        private void btSpeichern_Click(object sender, RoutedEventArgs e)
        {

                if (textfeldRot(tbBreiteBitmap.BorderBrush.GetHashCode().ToString(), tbHoeheBitmap.BorderBrush.GetHashCode().ToString(), tbAnzahlKoordinaten.BorderBrush.GetHashCode().ToString()))
                {
                    MessageBox.Show("Leider beinhalten die Eingabefelder falsche Werte.\n\nVersuchen Sie es bitte erneut.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    try
                    {
                        Vorschau.Properties.Settings.Default.BreiteBitmap = Int32.Parse(tbBreiteBitmap.Text);
                        Vorschau.Properties.Settings.Default.HoeheBitmap = Int32.Parse(tbHoeheBitmap.Text);
                        Vorschau.Properties.Settings.Default.AnzahlKoordinaten = Int32.Parse(tbAnzahlKoordinaten.Text);
                        MessageBox.Show("Einstellungen wurden erfolgreich abgespeichert.", "Speicherung erfolgreich", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                    }
                    catch (Exception error)
                    {

                        MessageBox.Show("Unglücklicherweise trat beim Speichervorgang ein Fehler auf.\n\nVersuchen Sie es bitte erneut.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                        Debug.WriteLine("Error - beim Speichervorgang " + error);
                    }
                }            
        }

        private void tbBreiteBitmap_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (kontrolliereObZahl(tbBreiteBitmap.Text) == false)
                {
                    tbBreiteBitmap.BorderBrush = Brushes.Red;
                }
                else
                {
                    tbBreiteBitmap.BorderBrush = Brushes.LightGray;
                }
            }
            catch (Exception error)
            {
                Debug.WriteLine("Error 'tbBreitBitMap'" + error);
            }
        }

        private void tbHoeheBitmap_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (kontrolliereObZahl(tbHoeheBitmap.Text) == false)
                {
                    tbHoeheBitmap.BorderBrush = Brushes.Red;
                }
                else
                {
                    tbHoeheBitmap.BorderBrush = Brushes.LightGray;
                }
            }
            catch (Exception error)
            {
                Debug.WriteLine("Error 'tbHoeheBitmap'" + error);
            }
        }

        private void tbAnzahlKoordinaten_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (kontrolliereObZahl(tbAnzahlKoordinaten.Text) == false)
                {
                    tbAnzahlKoordinaten.BorderBrush = Brushes.Red;
                }
                else
                {
                    tbAnzahlKoordinaten.BorderBrush = Brushes.LightGray;
                }
            }
            catch (Exception error)
            {
                Debug.WriteLine("Error 'tbAnzahlKoordinaten'" + error);
            }
        }
    }
}

主窗口

<UserControl x:Class="Vorschau.UCEinstellung"
             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:Vorschau"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Margin="0,0,-326,-161">
        <TextBox x:Name="tbBreiteBitmap" HorizontalAlignment="Left" Height="23" Margin="145,38,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="85" LostFocus="tbBreiteBitmap_LostFocus"/>
        <TextBox x:Name="tbHoeheBitmap" HorizontalAlignment="Left" Height="23" Margin="145,83,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="85" LostFocus="tbHoeheBitmap_LostFocus"/>
        <Label x:Name="label" Content="Breite:" HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top"/>
        <Label x:Name="label_Copy" Content="Höhe:" HorizontalAlignment="Left" Margin="10,77,0,0" VerticalAlignment="Top"/>
        <Label x:Name="label_Copy1" Content="in PX" HorizontalAlignment="Left" Margin="247,35,0,0" VerticalAlignment="Top"/>
        <Label x:Name="label_Copy2" Content="in PX" HorizontalAlignment="Left" Margin="247,77,0,0" VerticalAlignment="Top"/>
        <Label x:Name="label1" Content="einstellbare Größe des Bitmaps" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Label x:Name="label1_Copy" Content="einstellbare Zahl der geenerierten Koordinaten" HorizontalAlignment="Left" Margin="10,172,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Label x:Name="label_Copy3" Content="Anzahl Koordinaten:" HorizontalAlignment="Left" Margin="10,203,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="tbAnzahlKoordinaten" HorizontalAlignment="Left" Height="23" Margin="145,203,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="85" LostFocus="tbAnzahlKoordinaten_LostFocus"/>
        <Image x:Name="image" HorizontalAlignment="Left" Height="350" Margin="289,10,0,0" VerticalAlignment="Top" Width="350" Source="img/imageedit_1_9734874017.png" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleY="1"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
        <Label x:Name="label1_Copy1" Content="Wertebreich des Koordinatensystems" HorizontalAlignment="Left" Margin="323,5,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Canvas HorizontalAlignment="Left" Height="405" Margin="299,23,0,0" VerticalAlignment="Top" Width="3" Background="#FFB8B8B8" RenderTransformOrigin="0.5,0.5">
            <Canvas.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="-1"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Canvas.RenderTransform>
        </Canvas>
        <Button x:Name="btSpeichern" Content="Speichern" HorizontalAlignment="Left" Margin="30,418,0,0" VerticalAlignment="Top" Width="75" Click="btSpeichern_Click"/>
    </Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vorschau
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        UCEinstellung einstellung = new UCEinstellung();
        UCVorschau vorschau = new UCVorschau();

        public MainWindow()
        {
            InitializeComponent();
            MouseDown += Window_MouseDown;

            einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
            einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
            einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();

        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                DragMove();
        }

        private void lbClose_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void lbMinimize_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            lbWhereIAm.Content = "Vorschau";
            conCon.Content = vorschau;
        }

        private void btEinstellung_Click(object sender, RoutedEventArgs e)
        {
            lbWhereIAm.Content = "Einstellung";
            einstellung.tbBreiteBitmap.Text = "Test";
            conCon.Content = einstellung;
            einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
            einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
            einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vorschau
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        UCEinstellung einstellung = new UCEinstellung();
        UCVorschau vorschau = new UCVorschau();

        public MainWindow()
        {
            InitializeComponent();
            MouseDown += Window_MouseDown;

            einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
            einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
            einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();

        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                DragMove();
        }

        private void lbClose_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void lbMinimize_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            lbWhereIAm.Content = "Vorschau";
            conCon.Content = vorschau;
        }

        private void btEinstellung_Click(object sender, RoutedEventArgs e)
        {
            lbWhereIAm.Content = "Einstellung";
            einstellung.tbBreiteBitmap.Text = "Test";
            conCon.Content = einstellung;
            einstellung.tbBreiteBitmap.Text = Vorschau.Properties.Settings.Default.BreiteBitmap.ToString();
            einstellung.tbHoeheBitmap.Text = Vorschau.Properties.Settings.Default.HoeheBitmap.ToString();
            einstellung.tbAnzahlKoordinaten.Text = Vorschau.Properties.Settings.Default.AnzahlKoordinaten.ToString();

        }
    }
}

1 个答案:

答案 0 :(得分:4)

看起来您只是分配属性而不是保存它们。您需要调用Save方法

Vorschau.Properties.Settings.Default.Save();

保存后,您可以在%userprofile%\appdata\local下找到该文件。有关详细信息,请参阅How to load a PKCS#12 Digital Certificate with Javascript WebCrypto API

为此,设置范围应为“用户”。