|
Hello everyone
In my WPF project I have created a UserControl (this is called Applic.xaml), and within this usercontrol I have placed another UserControl (this is called PasswordScreen.xaml). Here is the xaml design code:
<UserControl xmlns:my2="clr-namespace:iPosCloud.com.iposcloud.uiC_.views"
x:Class="iPosCloud.com.iposcloud.main.Applic"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my0="clr-namespace:iPosCloud.com.iposcloud.main"
xmlns:Intersoft="http://intersoft.clientui.com/schemas"
xmlns:my1="clr-namespace:Intersoft.Client.UI.Controls;assembly=Intersoft.Client.UI.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="1024" xmlns:my="http://schemas.microsoft.com/surface/2008">
<UserControl.Background>
<ImageBrush ImageSource="/iPosCloud;component/images/HoneyComb.png" Stretch="Fill" TileMode="Tile" Viewport="0,0,46,25" ViewportUnits="Absolute" />
</UserControl.Background>
<UserControl.Resources>
<DropShadowEffect x:Key="DropShadowEffect" ShadowDepth="0.1" Direction="360" BlurRadius="3" Opacity=".5" />
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="MainGridApplic">
<Border BorderThickness="2" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Stretch" Margin="4" BorderBrush="{x:Null}">
<Border.BitmapEffect>
<DropShadowBitmapEffect Color="Black" Direction="360" ShadowDepth="10" Opacity=".5" Softness="9" />
</Border.BitmapEffect>
<Intersoft:UXGridPanel Margin="2" Column="1" Row="1" ItemHeight="220" ItemWidth="1190" Name="UXGridPanelLeft">
<Image Name="image1" Source="/iPosCloud;component/images/Logo.png" Width="1000" HorizontalAlignment="Left" />
</Intersoft:UXGridPanel>
</Border>
<Grid Margin="2" HorizontalAlignment="Right">
<my2:PasswordScreen /> "HERE IS THE UserControl THAT I PLACED..."
</Grid>
</Grid>
</UserControl>
In PasswordScreen.xaml there is a button (called Login). On click event when clicked I want to call another xmal file called SwitchboardView.xaml but not firing nothing and the code behind this button is:
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
SwitchboardView sbv = new SwitchboardView();
Applic ap = new Applic();
ap.MainGridApplic.Children.Add(sbv);
}
Could someone please help me solve this problem (what am I doing wrong...)
Kind regards
Lapeci
|
|
|
|
|
For starters you're not modifying the existing Applic UserControl.
You are creating a new one and setting the SwitchboardView into that UserControl.
Then you're dropping them both on the floor!
A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.
|
|
|
|
|
Hello Matt
Thank you for replying to my question.
I am really struggling on this could you please if you can help me to solve this problem, I'm new to wpf and c# honestly.
Kind regards
Lapeci
|
|
|
|
|
Hello
I am playing around with the async and await keywords using .net4.5 and windows 7.
The problem is that I would like to use RenderTargetBitmap and System.Windows.Media.Imaging.BitmapEncoder to capture a UI element and return a System.Drawing.Bitmap.
The following is some test code. The problem is that it does a lot of work synchronously (e.g. using FileStream and BitmapEnocder).
For this reason, the UI blocks most of the time e.g. the UI is non-responsive.
I was hoping some of the more experienced programmers here could advise me on where I have gone wrong or how best to proceed with this function?
Many thanks for your time....
private async Task<System.Drawing.Bitmap> GetCaptureBitmap(CaptureFrame captureFrame)
{
VisualBrush brush = new VisualBrush(captureFrame);
brush.Stretch = Stretch.Fill;
brush.AlignmentX = AlignmentX.Left;
brush.AlignmentY = AlignmentY.Top;
brush.TileMode = TileMode.None;
Rectangle rect = new Rectangle();
rect.Width = Settings.Instance.Capture.Width;
rect.Height = Settings.Instance.Capture.Height;
rect.Fill = brush;
rect.Measure(new Size(rect.Width, rect.Height));
rect.Arrange(new Rect(0, 0, rect.Width, rect.Height));
System.Windows.Media.Imaging.RenderTargetBitmap bmp = new System.Windows.Media.Imaging.RenderTargetBitmap((int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(rect);
FileStream stream = new FileStream(System.IO.Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose);
System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
encoder.Save(stream);
stream.Flush();
System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(stream, true, false);
return bitmap;
}
|
|
|
|
|
I did not notice an "await" in your method, so the whole method runs synchronously.
Consider using a BackgroundWorker instead.
The "do work" handler of the BackgroundWorker can do the work in your method; the "completed" event handler can subsequently update the UI, if necessary.
|
|
|
|
|
Hi
Thanks for your advice. I think that you are right. However, this series of functions creates UI controls and then uses rendertargetbitmap to save an image of the control to the disk. I am thinking that I might need to create a UI thread....
I am now going to investigate if it is possible to create a UI thread but some basic searches seems to suggest that it is...
I have used the backgroundworker before but not for UI work. Basically what I want to do is pass some parameters to a thread and for the thread to create the UI controls independently of the main application UI thread.
Hope this makes sense but I am trying to learn this stuff for the first time....I could easily be going about this wrong.
Many thanks....
|
|
|
|
|
If this is a "windows" application, the first / main thread is typically the "UI thread." (You cannot create more than one UI thread).
Besides the "completed" event, which runs on the UI thread, the BackgroundWorker also has a "progress changed" event handler that can be invoked from the "do work" handler via the "on progress changed method"; this "progress changed" handler runs on the UI thread so you can update the UI there.
When using "progress changed" you need to insure that:
- the UI thread is available for "progress changed" to run
- "progress changed" may need to be re-entrant depending on how often / frequently you call it from "do work".
And passing parameters to your threads may not always be practical; you can however "share" memory safely between threads (except UI controls): see "collections.concurrent" and "interlocked" for "non-blocking" possible solutions.
(It's not that something is wrong; some things are just easier than others; and there is usually more than one way).
In general, the BackgroundWorker is suggested if a thread needs to interact with the UI (i.e. via progress changed).
|
|
|
|
|
Technically, you can have more than one UI thread. It's not recommended but it is possible.
|
|
|
|
|
Thanks for that.
Sometimes I equate "shouldn't" with "can't" ...
(Hmmm; now let's see where I can use multiple UI threads in "my" apps).
Yeah, but .... I think we were talking "one window". A window can only have one UI thread.
Multiple windows; one thread for each window; ok.
|
|
|
|
|
Yeah, one window one UI thread.
|
|
|
|
|
Thank you for your help.
I have been researching this and developing some test code.
The thing is that what I am doing is creating a series of bitmaps based upon the state of a user control. The user control displays an image or video and draws some shapes such as lines and rectangles and ellipses. I want to capture the state of the user control several times a second using a visualbrush and use rendertargetbitmap to save to bitmap on the disk.
This is a length process. With lengthy processes, it is recommended to use a background worker. However, I need some way of sharing the visualbrush with the backgroundworker thread. When I search google, people are saying try to refactor the code to use background worker. I am not sure how to do this as I need the background worker to have access to the UI elements. I think I need to use a single-threaded apartment (STA) thread. However, background workers are not STA?
The alternative of creating a new window is possible and using the windows UI thread. However, it is not advised to do lengthy process on a UI thread. I would need to pump for windows messages (I am getting debug warning in visual studio).
So I am not sure what is the best way forward...I might be missing something obvious....
Many thanks....
|
|
|
|
|
I have a rather complex user control for which I use RenderTargetBitmap to create an image that I can "print preview" or "print"; it takes an average of 700ms to render the image (no "saving").
On the other hand, I can do a full screen capture (using System.Drawing.Graphics) AND save the bitmap in about 120ms (where saving is probably the lengthy process).
If I was required to render my user control "several times a second" using RenderTargetBitmap, I would need a multi-core machine dedicated to just that.
Use multiple windows: one for user interaction, and the others for rendering.
|
|
|
|
|
What of the many MVVM frameworks would be recommended for a team that wants to start developing WPF/SL apps?
As a background the team is mainly experienced in VB6/Access VBA, and a lot of TSQL knowledge as well.
Obviously we have a lot to learn, but I'm not sure we want to start on the wrong step and start with the code-behind method.
At the moment I'm leaning toward Prism, but mainly because it seems to have the most complete documentation.
Or will we be taking a step to far, and should stick to code-behind until we have the experience?
|
|
|
|
|
MVVM Light now comes with training from Pluralsight. It's a good place to start.
|
|
|
|
|
I saw that, have you done the course? MVVMLight looked fairly good, enough to do it, without too much to digest. The VS snippets and templates where a nice help too.
|
|
|
|
|
I haven't done the course - primarily because I know MVVM Light pretty much inside out, and Laurent is using some of my codebase in there already. That reminds me, I really must poke him to update the attribution list to point back to my entries.
|
|
|
|
|
Have you heard about:
Catel
|
|
|
|
|
Thanks, looking at it now, certainly seems to be aimed at the type of apps we'll be creating.
I like the emphasis on documentation too.
|
|
|
|
|
I'm another vote for MVVM Lite from Galasoft, we were lucky enough to start with that before most of the others appeared.
You do know the Silverlight is dead, MS are no longer extending it and it has and EOL in 2022 or thereabouts.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I am worried about Silverlight, but has there actually been an official announcement of its EOL? I was leaning towards Silverlight as it can do desktop client and also be easily web hosted too.
Given that most of our apps are Access 97 and VB6, and were still on XP...with Win7 maybe appearing next year. The Win Store apps are a no go, and I've not seen any actual Win Store apps that aren't glorified web pages.
|
|
|
|
|
I'm pretty sure there has been an official announcement of the EOL.
If you don't actually need the web browser IE the apps are completely behind the firewall the WPF/WCF/Click once is a solution, it is the one I will be pushing. The reason we went to Silverlight was that it allows a very rich UI, the bonus is that once you have a reasonable depth on it is it FAST. It took us a year to get really conversant and develop our framework (code generator for stored procs and the WCF, build up a good set of snippets and styles, standardise our navigation) which allows us to push out an app at least twice as fast as an MVC developer and the UI is mush richer and faster performance.
The idea that you can code once and deploy on multiple form factors is still a ways off I believe, so I'm comfortable with WPF and a separate code base for any mobile requirements that come along. If you wxpect a mobile requirement it may be worth researching your data transport style (collections/json/odata etc) to make sure you don't end up needing a different service layer for mobile.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
First of all, what type of application are you creating?
Do you know why you are doing MVVM? A lot of people throw around 'it's great/separation of concerns etc.' without properly taking advantage of underlying strengths of the pattern.
Unless you are creating massive applications with big managed teams, skip Prism. I'd go with Caliburn Micro or MVVMLight..
Before making a decision you should create a basic application with a couple of forms, one with one to many/parent child data, a lot of buttons/menus and throw in some mouse/drag click events. This may sound trivial from a traditional Access/VB6 form perspective but can take a lot of work to function successfully in MVVM.
You might want to look at this library I did to take out a lot of the pain associated with MVVM https://github.com/steinborge/ProxyTypeHelper
While not strictly a framework it greatly simplify view model creation. It works with PRISM and should work with other frameworks. It still maintains full MVVM i.e. no strong couple of view or view model.
The following code demonstrates a viewmodel using the library, using the traditional method would be a lot more code.
public string FirstName {get;set;}
public string LastName {get;set;}
[LinkToProperty("FirstName")] [LinkToProperty("LastName")]
public string FullName
{
get
{
return String.Format("{0} {1}", FirstName, LastName);
}
}
[PropertyChangedEvent]
void person_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
}
[LinkToCommand("RollBackChangesCommand")]
private void btnRollBackChanges()
{
}
[LinkToEvent("AddingNewItemEvent")]
private void DataGrid_AddingNewItem(AddingNewItemEventArgs e)
{
}
modified 19-Jun-14 0:35am.
|
|
|
|
|
The apps are usually basic LoB CRUD apps, outputting to word/excel is fairly common, occasional imports of raw data etc.
Most a small team users, though there are a few where that team is spread about the country.
Their lifetime is fairly long with not a massive amount of regular change, most apps will be <1 release per year, and some have never changed in the 10 years I've worked here.
I have seen others that point out for simple apps MVVM isn't a requirement, but I don't think we're big enough to support multiple types of approach. So although MVVM could be overkill for some apps, at least they'd all be consistent.
|
|
|
|
|
Here are a few more thoughts..
You don't need any of the aforementioned libraries to implement MVVM - it's just without some of their features UI interaction (even something as a simple as displaying a message box) and communication between modules can be difficult.
Another option is WPF Application Foundation. https://waf.codeplex.com
It's not being supported but provides some excellent documentation and samples on a concept of passing a IView interface to the view model:
https://waf.codeplex.com/wikipage?title=Model-View-ViewModel%20Pattern&referringTitle=Home
This allows you to call view (UI) operations from the view model without violating MVVM.
My experience with Prism is version 4, which has been out for years. It's kinda of a all or nothing approach. But Prism 5 breaks it out into separate parts - so you could use the MVVM library and not the Composition library. And as you mentioned it has excellent documentation.
|
|
|
|
|
Yes, in theory you don't need anything. My experience with this is that people that go this way end up in rubbish applications because they fall in exactly the same pitfalls everyone falls into. Then the people that recommended that you didn't need anything aren't there to help you out. Please, for your own sake, pick a framework that fits your application. Don't try to re-invent the wheel, these frameworks / toolkits / whatever you like to call them are out there for years and being used in production for years. Just pick one that you like and have a good feeling with and that fits you use-cases.
If you have a light-weight app, mvvm light can do everything you need and is probably a good option. If you need more (such as authentication, behaviors, nested user controls, resolve by naming convention, etc), pick something that supports that (for example, Catel[^]).
Another tip: don't pick an abandoned project if you want to use if for LoB applications. You'll end up fixing someone else's code that isn't maintained any longer.
|
|
|
|