'MouseEventArgs' does not contain a definition for 'GetPosition' - c#

Im sure this is a super easy one but I am working on a C# UWP app and am trying to make a drag-able user control but in my mouse event I'm getting the following error:
CS1061 'MouseEventArgs' does not contain a definition for 'GetPosition' and no extension method 'GetPosition' accepting a first argument of type 'MouseEventArgs' could be found (are you missing a using directive or an assembly reference?)
I found this example I am using on stack (Dragging a WPF user control) and have used mouse down events before for dragging items between list boxes in winforms but didn't have this issue.
Here is my code:
<UserControl
x:Class="HopHaus.TimerControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HopHaus"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400" Width="120" Height="60">
<Grid Margin="0,0,0,167" Width="120">
<Rectangle Fill="#FF404040" HorizontalAlignment="Left" Height="60" Margin="1,0,-1,-133" Stroke="Black" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="timersetBox" Margin="-1,-2,1,-59" TextWrapping="Wrap" Text="00" VerticalAlignment="Top" Height="60" BorderBrush="Transparent" FontFamily="Fonts/DIGITAL.TTF#Digital" Foreground="#FF72FB00" FontSize="50" Background="Transparent" TextAlignment="Right" AcceptsReturn="True" SelectionHighlightColor="#000078D7" Width="120"/>
<Border x:Name="dragBrdr" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="59" Margin="2,0,0,-59" VerticalAlignment="Top" Width="118"/>
</Grid>
</UserControl>
And:
public sealed partial class TimerControl : UserControl
{
Point currentPoint;
Point anchorPoint;
bool isInDrag;
public TimerControl()
{
this.InitializeComponent();
}
private TranslateTransform transform = new TranslateTransform();
private void root_MouseMove(object sender, MouseEventArgs e)
{
if (isInDrag)
{
var element = sender as FrameworkElement;
currentPoint = e.GetPosition(null);
transform.X += currentPoint.X - anchorPoint.X;
transform.Y += (currentPoint.Y - anchorPoint.Y);
this.RenderTransform = transform;
anchorPoint = currentPoint;
}
}
}
I am using both using System.Windows.Input & using Windows.Devices.Input
Thanks a bunch in advance for any help provide.

To have better support for touch and inking there is an abstraction over the mouse events. This is called pointer. So in you you have a PointerMoved event
in xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
PointerMoved="Grid_PointerMoved">
</Grid>
and in code
private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
{
var point = e.GetCurrentPoint(null);
}

but am still unsure how to take my var point and transform it into a new position of my user control on my MainPage
As #Dave Smits said you need a Pointer event handle. More details about handle pointer input please reference this document. I think what you confused is that this code var point = e.GetCurrentPoint(null); return PointerPoint object instead Point structure what you needed for transform. In that case, you can get the Point structure by PointerPoint.Position property. Updated code as follows:
<UserControl
...
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
Width="120" Height="60" PointerMoved="Grid_PointerMoved" CanDrag="True">
<Grid Margin="0,0,0,167" Width="120" >
<Rectangle Fill="#FF404040" HorizontalAlignment="Left" Height="60" Margin="1,0,-1,-133" Stroke="Black" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="timersetBox" Margin="-1,-2,1,-59" TextWrapping="Wrap" Text="00" VerticalAlignment="Top" Height="60" BorderBrush="Transparent" FontFamily="Fonts/DIGITAL.TTF#Digital" Foreground="#FF72FB00" FontSize="50" Background="Transparent" TextAlignment="Right" AcceptsReturn="True" SelectionHighlightColor="#000078D7" Width="120"/>
<Border x:Name="dragBrdr" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="59" Margin="2,0,0,-59" VerticalAlignment="Top" Width="118"/>
</Grid>
</UserControl>
Code behind
Point currentPoint;
Point anchorPoint;
bool isInDrag;
private TranslateTransform transform = new TranslateTransform();
private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
{
anchorPoint = new Point(300, 200);
isInDrag = true;
if (isInDrag)
{
var element = sender as FrameworkElement;
PointerPoint currentPointpointer = e.GetCurrentPoint(null);
currentPoint = currentPointpointer.Position;
transform.X += currentPoint.X - anchorPoint.X;
transform.Y += (currentPoint.Y - anchorPoint.Y);
this.RenderTransform = transform;
anchorPoint = currentPoint;
}
}
Additionally, for transform from one point to another I recommend you to use PointAnimation.

Related

Drag and Drop does not work in UWP app on Windows 10 Mobile

I develop UWP app with C# and XAML. I try to implement drag&drop functionality.
Here is a simple app to demonstrate how I try to do it. The app has two borders - one border is dragged, and second one is for drop. Also I write informaton about events to output.
Version 1 (using CanDrag=true on a dragged item)
<Page x:Class="UWP_DragDrop.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP_DragDrop"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="draggedItem"
Grid.Row="0"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Red"
CanDrag="True"
DragStarting="draggedItem_DragStarting"
DropCompleted="draggedItem_DropCompleted">
<TextBlock Text="Drag this item"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<Border x:Name="dropArea"
Grid.Row="1"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Blue"
AllowDrop="True"
DragEnter="dropArea_DragEnter"
Drop="dropArea_Drop">
<TextBlock Text="Drop here"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</Grid>
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace UWP_DragDrop
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void draggedItem_DragStarting(UIElement sender, DragStartingEventArgs args)
{
Debug.WriteLine("draggedItem_DragStarting");
}
private void draggedItem_DropCompleted(UIElement sender, DropCompletedEventArgs args)
{
Debug.WriteLine("draggedItem_DropCompleted");
}
private void dropArea_DragEnter(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_DragEnter");
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
}
private void dropArea_Drop(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_Drop");
}
}
}
1) It works correctly when I run it in Visual Studio for LocalMachine and for Simulator (but only for mouse input). In output I have the following:
draggedItem_DragStarting
dropArea_DragEnter
dropArea_Drop
draggedItem_DropCompleted
2) When I try to run it in Simulator (touch mode) - I can't drag item. No one event is fired (output is empty)
3) When I try to run it in Windows 10 Mobile emulator, it does not work. What I see in output is:
draggedItem_DragStarting
draggedItem_DropCompleted
As soon as I move the element - DropCompleted event fires.
Version 2 (using StartDragAsync)
I removed CanDrag=true for dragged item, and start drag operation by StartDragAsync.
<Page x:Class="UWP_DragDrop.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP_DragDrop"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="draggedItem"
Grid.Row="0"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Red"
PointerMoved="draggedItem_PointerMoved"
DragStarting="draggedItem_DragStarting">
<TextBlock Text="Drag this item"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<Border x:Name="dropArea"
Grid.Row="1"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Blue"
AllowDrop="True"
DragEnter="dropArea_DragEnter"
Drop="dropArea_Drop">
<TextBlock Text="Drop here"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</Grid>
using System.Diagnostics;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace UWP_DragDrop
{
public sealed partial class MainPage : Page
{
IAsyncOperation<DataPackageOperation> _dragOperation;
public MainPage()
{
this.InitializeComponent();
}
private void draggedItem_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && (_dragOperation == null))
{
Debug.WriteLine("draggedItem_StartDragAsync");
_dragOperation = draggedItem.StartDragAsync(e.GetCurrentPoint(draggedItem));
_dragOperation.Completed = DragCompleted;
}
}
private void DragCompleted(IAsyncOperation<DataPackageOperation> asyncInfo, AsyncStatus asyncStatus)
{
_dragOperation = null;
Debug.WriteLine("draggedItem_DragCompleted");
}
private void draggedItem_DragStarting(UIElement sender, DragStartingEventArgs args)
{
Debug.WriteLine("draggedItem_DragStarting");
}
private void dropArea_DragEnter(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_DragEnter");
e.AcceptedOperation = DataPackageOperation.Copy;
}
private void dropArea_Drop(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_Drop");
}
}
}
1) Desktop and Simulator (mouse mode) works.
2) Simulator (touch mode) now works too.
3) Windows 10 mobile emulator does not work. DropCompleter fires right after DragStarting again.
How can I make drag&drop work on Windows 10 mobile? Why version 1 does not work in scenario 2 and 3, and version 2 does not work in scenario 3?
Unfortunately, it seems like customize drag is currently not supported on emulator and mobile yet. Details please reference this known issue. As Raymond mentioned:
You can drag/drop items within a listview, and any UI element can be a drop target, but you don't get dragging to another process or drag customization.
At this time windows 10 doesn't support a full-fledged drag and drop feature.

Initialization of 'System.Windows.Controls.Button' threw an exception

I am creating a WPF on which, i have 2 threads. One is the main, and the other one(called PillTimeOutChecker) check some requirements in the main form. If the requirements meet, a new form through the PilleCheckerThread show up in a new thread. The problem is that i am getting this error: Initialization of 'System.Windows.Controls.Button' threw an exception in the initialization of the new form.
This is the method in the main thread, that call the PillCheckerThread:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread PillChecker = new Thread(new ThreadStart(PillCheckerThread));
PillChecker.SetApartmentState(ApartmentState.STA);
PillChecker.IsBackground = true;
PillChecker.Name = "PillTimeOutChecker";
PillChecker.Start();
}
This is the content of the PillCheckerThread method:
private void PillCheckerThread()
{
foreach (DataGridObject item in PillList.Items)
{
if(item.DoIt)
{
//Show PillWarning window
Thread PillWarningWindow = new Thread(new ThreadStart(() =>
{
PillWarningWindow pl = new PillWarningWindow(item.PillName, item.AlertTime);
pl.Show();
System.Windows.Threading.Dispatcher.Run();
}));
PillWarningWindow.SetApartmentState(ApartmentState.STA);
PillWarningWindow.IsBackground = true;
PillWarningWindow.Start();
}
}
}
This is the content of the PillWarningWindow:
public partial class PillWarningWindow : Window
{
public PillWarningWindow(string PillName, string CurrentTime)
{
InitializeComponent();
PillNameLbl.Content = PillName;
TimeLbl.Content = CurrentTime;
}
private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
This is the xaml of the PillWarningWindow:
<Window x:Class="MedicalReminder.PillWarningWindow"
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:MedicalReminder"
mc:Ignorable="d" Height="300" Width="713.414" ShowInTaskbar="False" Topmost="True" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="Transparent" AllowsTransparency="True">
<Border BorderBrush="Black" CornerRadius="20" Background="DarkCyan">
<Grid>
<Button x:Name="CloseBtn" Content="OK" HorizontalAlignment="Left" Margin="262,239,0,0" VerticalAlignment="Top" Width="179" Click="CloseBtn_Click"/>
<Label x:Name="label" Content="Psssttt !! It's time to take your pill: " HorizontalAlignment="Left" Margin="89,93,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/>
<Label x:Name="PillNameLbl" Content="PillName" HorizontalAlignment="Left" Margin="395,93,0,0" VerticalAlignment="Top" FontSize="18" FontWeight="Bold"/>
<Label x:Name="label2" Content="It's exactly " HorizontalAlignment="Left" Margin="89,132,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/>
<Label x:Name="TimeLbl" Content="12:00" HorizontalAlignment="Left" Margin="195,133,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="17" Width="56"/>
<Label x:Name="label3" Content="you forgot it ??" HorizontalAlignment="Left" Margin="256,132,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/>
</Grid>
</Border>
With a breakpoint at the constructor of the PillWarningWindow i found out that the error start at the InitializeComponent method. Any help is appreciated. Thanks.
If you want to access windows propety you should use Dispatchers.
Because of another thread cannot acces window directly. You can use the Dispatcher when you want to run an operation that will be executed on the UI thread.
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action(() => PillNameLbl.Content = PillName;
TimeLbl.Content = CurrentTime));
If debugger stops at InitializeComponent method, I'd say there is probably something wrong with your XAML code (which despite of being full of bad practices, doesn't seem to be incorrect). Are you sure your controls don't have style resources defined in any other place? I have had the same error before and it was caused by defining styles in a wrong order.
Try to launch your window directly at the application start instead of launching it from another thread, just to test if it's OK. If it works, then the problem is in the thread management.

how to a zoom option to my wpf app?

I'm working on wpf application , the application draws the countours on an existing image and i want to add a button which give me the hand to zoom the image to see the countours
i added a button and a fonction on .cs but it doesn't work
here's the code cs
namespace AnimationTest
{
public partial class MainWindow: Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = 1000;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
image1.BeginAnimation(ScaleTransform.CenterXProperty, da);
}
}
}
and XAML Code
<Window x:Class="AnimationTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="720" Width="1280">
<Grid>
<Image Height="681" HorizontalAlignment="Left" Name="image1" Stretch="None" VerticalAlignment="Top" Width="1258" Source="/AnimationTest;component/Images/world.jpg" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="1171,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
what's the problem ? and how can i fixe it ?
thanks
This has no sens image1.BeginAnimation(ScaleTransform.CenterXProperty, da); You start an animation on an image of a property that don't belong to Image control. You should create a scale transform, set it on the image and then start the animation on scale transform object, not the image.

How to make the canvas detect touch input properly in C#?

Basically, I'm trying to make the canvas listen for a touch input (tap) and will increment the number of taps on screen. It isn't working when I touch the screen on my device. I debugged my code and nothing seems out of the ordinary except that the touch is not detected. I checked ZIndex and the canvas is in front of the screen to be touchable. How do I make it work?
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Name="counter" FontSize="150" HorizontalAlignment="Center" TextWrapping="Wrap" Text="0" VerticalAlignment="Center" Margin="188,10,187,397"/>
<Button Content="Reset" HorizontalAlignment="Stretch" Margin="-18,535,-18,0" VerticalAlignment="Top" Click="Button_Click"/>
<Canvas ZIndex="0" Name="Canvas" HorizontalAlignment="Center" Height="535" VerticalAlignment="Top" Width="446" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" MouseLeftButtonUp="Canvas_MouseLeftButtonUp" MouseLeave="Canvas_MouseLeave"/>
</Grid>
C#:
int taps = 0; // create var to detect number of times, user touches the screen
// Constructor
public MainPage()
{
InitializeComponent();
}
// method to register the touch as the finger is placed on the screen
private void Canvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//Canvas c = sender as Canvas;
counter.Text = "TOUCHED!";
}
//method register the touch as the finger is lifting up from the screen
private void Canvas_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//Canvas c = sender as Canvas;
taps++;
counter.Text = taps.ToString(); //convert var from int to string
}
//method register the touch as the finger leaves the area of the screen
private void Canvas_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
//Canvas c = sender as Canvas;
MessageBox.Show("You left the screen without lifting your finger. That does not count as a tap!", "Caution!", MessageBoxButton.OK);
}
// method to reset the counter to zero when button is pressed and released
private void Button_Click(object sender, RoutedEventArgs e)
{
taps = 0; // reset the count
counter.Text = taps.ToString(); // convert var from int to string
}
I don't know why you want to do it with Canvas - it won't work as you have nothing in this Canvas, so it can't register your click/tap, Canvas is also hard to adjust to screen. I think it can be done simpler way if you want to do it with MouseUp/Down - subscribe directly to Grid containing your elements instead of filling this Grid with additional Canvas:
In XAML:
<Grid x:Name="ContentPanel" Margin="12,0,12,0" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="7*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<TextBlock Name="counter" FontSize="150" HorizontalAlignment="Center" TextWrapping="Wrap" Text="0" VerticalAlignment="Center" Grid.Row="0"/>
<Button Content="Reset" HorizontalAlignment="Stretch" VerticalAlignment="Center" Click="Button_Click" Grid.Row="1"/>
<TextBlock Name="Touched" FontSize="50" HorizontalAlignment="Center" TextWrapping="Wrap" Text="Touched" VerticalAlignment="Center" Visibility="Collapsed" Grid.Row="2"/>
</Grid>
In code behind:
private int taps = 0;
public MainPage()
{
InitializeComponent();
ContentPanel.MouseLeftButtonDown += ContentPanel_MouseLeftButtonDown;
ContentPanel.MouseLeftButtonUp += ContentPanel_MouseLeftButtonUp;
}
private void ContentPanel_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
taps++;
counter.Text = taps.ToString(); //convert var from int to string
Touched.Visibility = Visibility.Collapsed;
}
private void ContentPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Touched.Visibility = Visibility.Visible;
}
// method to reset the counter to zero when button is pressed and released
private void Button_Click(object sender, RoutedEventArgs e)
{
taps = 0; // reset the count
counter.Text = taps.ToString(); // convert var from int to string
}
As you can see I've subscribed to Grid events (which covers whole screen) - but to make it work I had to set its Background Brush to Transparent, otherwise it will work only if you touch text.
There are many other ways to make your App work, but I hope this will help.
Is there a reason why you don't use the touch-events?
Instead of using MouseLeftButtonDown and MouseLeftButtonUp you should use TouchDown and TouchUp.
Only when you don't handle the touch events or the manipulation events they will be mapped to mouse events. In my experience with touch a single tap also not always gets mapped to MouseLeftButtonDown. As far as I know you could also with mouse events only recoginse one finger. When you want to count more fingers it's necessary to use the TouchDown/TouchUp events.
The problem lies in the overlapping style of the grid
so either make grid rows or define a stackpanel inside the grid, something like this.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<stackpanel>
<TextBlock Name="counter" FontSize="150" HorizontalAlignment="Center" TextWrapping="Wrap" Text="0" Margin="0,0,0,0"/>
<Canvas ZIndex="0" Name="Canvas" HorizontalAlignment="Center" Height="535" VerticalAlignment="Top" Width="446" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" MouseLeftButtonUp="Canvas_MouseLeftButtonUp" MouseLeave="Canvas_MouseLeave"/>
<Button Content="Reset" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Top" Click="Button_Click"/>
</stackpanel>
</Grid>
Try and check now.
You should set your Background property. If you don't want any background set it to Transparent:
<Canvas ZIndex="99" Background="Transparent" Name="Canvas" HorizontalAlignment="Center" Height="535" VerticalAlignment="Top" Width="446" Tapped="Canvas_CountMyTaps"/>
(If you want the canvas to be on Top be sure to make it have a greater ZIndex than the other elements that it overlaps)
If not set (the default value is null) the element won't capture any taps/click etc, it will be as if they "fall through".
Also, consider using the Tapped event which is a "higher level" event that will respond to clicks, taps with the finger, stylus, etc.

C# User Must Add Multiple Buttons to WPF and Buttons Must Persist

I am attempting to create a program in which the User can create multiple profiles. These profiles can be accessed via buttons that appear as each profile is completed.
My problem:
I have no clue how to make the created buttons persist after the program is exited(I need to save the buttons?)
Visually, this is program's process: 1) Enter your information, click continue 2) View a display page of what you entered, click done. 3) This adds a button to the final window, the button of course takes you to 4) Your profile you just created.
After this, the program ends and nothing is saved. I'm fairly new to c# and am quite confused on how to "save" multiple buttons without massively complicating the code. I'm a complete noob to c# and have a little Java experience. Am I going about this correctly? I'm pretty sure its possible but have no idea to go about it.
I will include my code below. I'm working in visual studios 2012. any help would be appreciated!
MainWindow XAML:
<Window x:Class="VendorMain.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="FirstName" HorizontalAlignment="Left" Margin="63,45,0,0" VerticalAlignment="Top"/>
<Label Content="LastName" HorizontalAlignment="Left" Margin="63,71,0,0" VerticalAlignment="Top"/>
<Label Content="Image" HorizontalAlignment="Left" Margin="63,102,0,0" VerticalAlignment="Top"/>
<Image Name="imgPhoto" Stretch="Fill" Margin="63,133,303,69"></Image>
<Button Name="UploadImageButton" Content="Upload Image" HorizontalAlignment="Left" Margin="130,105,0,0" VerticalAlignment="Top" Width="84" Click="UploadImageButton_Click"/>
<TextBox Name="AssignFirstName" Text="{Binding SettingFirstname}" HorizontalAlignment="Left" Height="23" Margin="130,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<TextBox Name="AssignLastName" Text="{Binding SettingLastName}" HorizontalAlignment="Left" Height="23" Margin="130,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Name="ContinueToDisplayWindow" Content="Continue" HorizontalAlignment="Left" Margin="409,288,0,0" VerticalAlignment="Top" Width="75" Click="ContinueToDisplayWindow_Click" />
</Grid>
MainWindow Code:
namespace VendorMain
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void UploadImageButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (op.ShowDialog() == true)
{
imgPhoto.Source = new BitmapImage(new System.Uri(op.FileName));
//SettingImage.Source = imgPhoto.Source;
}
}
private void ContinueToDisplayWindow_Click(object sender, RoutedEventArgs e)
{
DisplayPage displaypg = new DisplayPage();
displaypg.DpFirstName.Content = AssignFirstName.Text;
displaypg.DpLastName.Content = AssignLastName.Text;
displaypg.DpImage.Source = imgPhoto.Source;
displaypg.Show();
}
}
}
DisplayPage XAML:
<Window x:Class="VendorMain.DisplayPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DisplayPage" Height="300" Width="525">
<Grid>
<Label Name="DpFirstName" Content="{Binding getFirstNamePermenent}" HorizontalAlignment="Left" Margin="86,55,0,0" VerticalAlignment="Top"/>
<Label Name="DpLastName" Content="{Binding getLastNamePermenent}" HorizontalAlignment="Left" Margin="87,80,0,0" VerticalAlignment="Top"/>
<Image Name="DpImage" HorizontalAlignment="Left" Height="100" Margin="94,111,0,0" VerticalAlignment="Top" Width="100"/>
<Button Name="ButtonizeThisProfile_Button" Content="Done" HorizontalAlignment="Left" Margin="420,238,0,0" VerticalAlignment="Top" Width="75" Click="ButtonizeThisProfile_Button_Click"/>
</Grid>
DisplayPage Code:
namespace VendorMain
{
/// <summary>
/// Interaction logic for DisplayPage.xaml
/// </summary>
public partial class DisplayPage : Window
{
public Button bot1;
public DisplayPage()
{
InitializeComponent();
}
private void newBtn_Click(object sender, RoutedEventArgs e)
{
carryToFinalView();
}
private void ButtonizeThisProfile_Button_Click(object sender, RoutedEventArgs e)
{
UserProfiles uPro = new UserProfiles();
System.Windows.Controls.Button newBtn = new Button();
newBtn.Content = "Person1";
newBtn.Name = "NewProfileButtonAccess";
newBtn.Click += new RoutedEventHandler(newBtn_Click);
uPro.ButtonArea.Children.Add(newBtn);
uPro.Show();
}
public void carryToFinalView()
{
DisplayPage displaypg = new DisplayPage();
displaypg.DpFirstName.Content = DpFirstName.Content;
displaypg.DpLastName.Content = DpLastName.Content;
displaypg.DpImage.Source = DpImage.Source;
displaypg.Show();
}
}
}
UserProfile XAML:
<Window x:Class="VendorMain.UserProfiles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="UserProfiles" Height="300" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".8*" />
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="6*"/>
<RowDefinition Height="11*"/>
</Grid.RowDefinitions>
<Label Content="User Profiles: " HorizontalAlignment="Left" Margin="37,47,0,0" VerticalAlignment="Top"/>
<StackPanel Name="ButtonArea" Grid.Column="2" Grid.Row="2">
</StackPanel>
<Button Name="AddAnotherProfileButton" Content="Button" HorizontalAlignment="Left" Margin="35,146,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="AddAnotherProfileButton_Click"/>
</Grid>
UserProfile Code:
namespace VendorMain
{
public partial class UserProfiles : Window
{
public UserProfiles()
{
InitializeComponent();
}
private void AddAnotherProfileButton_Click(object sender, RoutedEventArgs e)
{
MainWindow mw = new MainWindow();
mw.Show();
}
}
}
As a self proclaimed 'noob', I fear that you won't receive an answer here. I certainly don't have time to repeatedly come back to answer a whole continuing stream of related questions. I also don't have time to provide you with a complete solution. However, I am happy to provide you with sort of 'pseudo code' to at least point you in the right direction... you will have to do a lot of this yourself.
So first things first, as mentioned in a comment, although it is possible, we don't generally save the UI Button objects, but instead we save the data that relates to the user profiles. Therefore, if you haven't done this already, create a User class that has all of the relevant properties. Implement the INotifyPropertyChanged Interface in it and add the SerializableAttribute to the class definition... this will enable you to save this class type as binary data.
Next, in your UI, don't add each Button in xaml... there's a better way. One way or another, add a collection property of type User or whatever your class is called, and set this as the ItemsSource of a ListBox. The idea here is to add a DataTemplate for your User type which will display each of the User items in the collection as a Button:
<DataTemplate x:Key="UserButtonTemplate" DataType="{x:Type DataTypes:User}">
<Button Text="{Binding Name}" Width="75" Click="AddAnotherProfileButton_Click" />
</DataTemplate>
You can find out more about DataTemplates in the Data Templates article.
Implementing this collection allows you to have and display any number of user profiles in your UI, rather than being restricted by screen size as your original example would be.
Now finally, on to saving the data... this can be achieved relatively simply using the following code:
try
{
using (Stream stream = File.Open("ProfileData.bin", FileMode.Create))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter .Serialize(stream, usersList);
}
}
catch { }
One thing to note is that WPF wants us to use the ObservableCollection<T> class when displaying data in the UI, but this class causes problems when serializing data with this method... therefore, you will need to convert your ObservableCollection<T> to a List<T> or similar. However, this can be easily achieved:
List<User> usersList = users.ToList();
You can find out how to de-serialize your data from the C# Serialize List tutorial. You would deserialize (or load the data from the saved file) each time your application starts and re-save the file each time the program closes. You can add an event handler to the Application.Deactivated Event or the Window.Closing which gets called when the application closes, so you can put your code to save the file in there.
Well, I took longer and wrote more than I had expected, so I hope that helps.

Categories

Resources