Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have defined a XAML-Window:
XML
<pre><Window x:Class="latex_curriculum_vitae.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:latex_curriculum_vitae"
        mc:Ignorable="d"
        Title="Latex Curriculum Vitae" Height="450" Width="800">
    <Grid>
        <Label Content="Jobtitle of your application:" HorizontalAlignment="Left" Margin="64,52,0,0" VerticalAlignment="Top"/>
        <Label Content="Enter Company Name:" HorizontalAlignment="Left" Margin="64,83,0,0" VerticalAlignment="Top"/>
        <Label Content="Enter companies street:" HorizontalAlignment="Left" Margin="64,109,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.52,-0.663"/>
        <Label Content="ZIP Code:" HorizontalAlignment="Left" Margin="64,135,0,0" VerticalAlignment="Top"/>
        <Label Content="City:" HorizontalAlignment="Left" Margin="475,135,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtJobtitle" HorizontalAlignment="Left" Margin="318,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="428"/>
        <TextBox x:Name="txtCompanyName" HorizontalAlignment="Left" Margin="318,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="428" RenderTransformOrigin="0.042,0.295"/>
        <TextBox x:Name="txtCompanyStreet" HorizontalAlignment="Left" Margin="318,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="428" RenderTransformOrigin="0.517,-0.986"/>
        <TextBox x:Name="txtZIP" HorizontalAlignment="Left" Margin="318,139,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="txtCity" HorizontalAlignment="Left" Margin="544,139,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="202"/>
        <Separator HorizontalAlignment="Left" Margin="64,166,0,0" VerticalAlignment="Top" Height="25" Width="701" RenderTransformOrigin="0.501,1.041"/>
        <Label Content="About Company" HorizontalAlignment="Left" Margin="64,21,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Label Content="About Contact person" HorizontalAlignment="Left" Margin="64,191,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Label Content="Name:" HorizontalAlignment="Left" Margin="356,237,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.655,0.05"/>
        <ComboBox x:Name="cboGender" HorizontalAlignment="Left" Margin="166,237,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.192,-0.613">
            <ComboBoxItem Content="Male"/>
            <ComboBoxItem Content="Female"/>
            <ComboBoxItem Content="Unknown"/>
        </ComboBox>
        <Label Content="Gender:" HorizontalAlignment="Left" Margin="64,235,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.401,0.29" Width="97"/>
        <TextBox x:Name="txtContactName" HorizontalAlignment="Left" Margin="431,241,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="315" RenderTransformOrigin="0.523,1.812"/>
        <Separator HorizontalAlignment="Left" Margin="64,268,0,0" VerticalAlignment="Top" Height="25" Width="701" RenderTransformOrigin="0.501,1.041"/>
        <Label Content="About Application" HorizontalAlignment="Left" Margin="64,293,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.783,-0.287" FontWeight="Bold"/>
        <Label Content="URL:" HorizontalAlignment="Left" Margin="64,324,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.105,-0.441"/>
        <TextBox x:Name="txtURL" HorizontalAlignment="Left" Margin="166,328,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="580"/>
        <Label Content="Emailaddress:" HorizontalAlignment="Left" Margin="64,355,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.171,-0.634"/>
        <TextBox x:Name="txtEmail" HorizontalAlignment="Left" Margin="166,359,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="266" RenderTransformOrigin="0.5,0.5">
            <TextBox.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="0.086"/>
                    <TranslateTransform/>
                </TransformGroup>
            </TextBox.RenderTransform>
        </TextBox>
        <Button x:Name="btnGenerate" Content="Generate" HorizontalAlignment="Left" Margin="549,384,0,0" VerticalAlignment="Top" Width="96"/>
        <Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" Margin="669,384,0,0" VerticalAlignment="Top" Width="96"/>


    </Grid>
</Window>

Also i have a class "User":
C#
class User
    {
        private String firstname;
        private String familyname;
        private String mystreet;
        private String myCity;
        private String myphone;
        private String myemail;
        private String myblog;

        public User()
        {


            firstname = ConfigurationManager.AppSettings.Get("firstname");
            familyname = ConfigurationManager.AppSettings.Get("familyname");
            mystreet = ConfigurationManager.AppSettings.Get("mystreet");
            myCity = ConfigurationManager.AppSettings.Get("mycity");
            myphone = ConfigurationManager.AppSettings.Get("myphone");
            myemail = ConfigurationManager.AppSettings.Get("myemail");
            myblog = ConfigurationManager.AppSettings.Get("myblog");

            DebugSettings();
            UserFile();
        }

        private void UserFile()
        {
            string[] lines = { "\\def\\firstname{" + firstname + "}", "\\def\\familyname{" + familyname + "}" , "\\def\\mystreet{" + mystreet + "}", "\\def\\mycity{" + myCity + "}", "\\def\\myphone{" + myphone + "}", "\\def\\myemail{" + myemail + "}", "\\def\\myblog{" + myblog + "}" };

            // Set a variable to the Documents path.
            string appDataPath =
              Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            // Write the string array to a new file named "WriteLines.txt".
            using (StreamWriter outputFile = new StreamWriter(Path.Combine(appDataPath, "personal_data.tex")))
            {
                foreach (string line in lines)
                    outputFile.WriteLine(line);
            }

        }

        private void DebugSettings()
        {
            string[] settings = { firstname, familyname, mystreet, myCity, myphone, myemail, myblog };

            foreach (string setting in settings)
                Console.WriteLine(setting);
        }

    }


In the MainWindow i use:

C#
private void btnGenerate_Click(object sender, EventArgs e)
        {
            User myuser = new User();
            
        }


In general i'm expecting after clicking on button "btnGenerate" that it runs the methods DebugSettings and UserFile.

But currently it looks like it doesn't outputs the settings nor it saves the file.

Have i missed anything?

What I have tried:

Also googled around, but nothing found :-(
Posted
Updated 17-Sep-20 0:53am
v2
Comments
Richard MacCutchan 17-Sep-20 6:53am    
You can check what happens when you riun it by using the debugger and stepping through the code. Note that a WPF application does not have a console, so none of those Console.WriteLine(setting); statements will do anything.

It's not enough to just put a method within your code, you have to tell WPF that you want to bind the button click in the XAML code as well. Take a look at this useful page[^] which explains how bind events to XAML controls.

You could also consider using a command[^] instead which would be bound to your view-model.
 
Share this answer
 
YOur btnGenerate_Clickmethod isn't hooked to the btnGenerate object in your XAML:
HTML
<Button x:Name="btnGenerate" Content="Generate" HorizontalAlignment="Left" Margin="549,384,0,0" VerticalAlignment="Top" Width="96"/>

You need to add Click = "btnGenerate_Click" to the XAML
 
Share this answer
 
Comments
Sascha Manns 17-Sep-20 7:09am    
Thanks to all. It works as expected :-)
OriginalGriff 17-Sep-20 7:15am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900