I have been trying to customize a webbrowser control in wp7. All I need to do is to add a border property(I think it is already available in webbrowser but I have to make it mandatory when I make it as a control) and want to add a few extra event handlers and make it as a control so that it can be used later in different projects.
I tried reading through materials in msdn for writing control template and it all revolves around writing a few xaml code and attaching it with codebehind to make it work. What I don't understand is how do I learn xaml? or in other words where is the reference for all the tags that msdn talks about in xaml? Itseems to be huge and I am not sure how to go about it. The tutorial in msdn straight away divulges into xaml code and I am totally confused..
So now all I am trying to do is this,
create a control in Expression Blend (Windows Phone Control Library) that would create a class (MyCustomControl) that inherits
from CustomControl base class.
Now in xaml I am adding a webbrowser control and adding four event handlers (mouseup,down etc.,).
I build this control in blend and add the corresponding dll in VS2010.
And now once I try to add this control to my wp7 phone application it says "Cannot create an instance of MyCustomControl"..
These were further links that I referenced in creating one,
Windowsphone - Creating a custom control
Windowsphone - Control Template.
UserControl vs Custom Control
Creating a new control by creating ControlTemplate
Any further help would be great.
There is no definitive list of 'elements' you can add to your XAML. The reason for this is that the XAML parser can create any class which is a UIElement based on the XML you provide. So the elements available to you depend on the assemblies present in your project. Read teh MSDN XAML Overview for details
For a list of controls that are present by default, take a look at the System.Windows.Controls namespace (I think this link is not for your version of Silverlight, it might be best to use the Object Browser to look at the assemblies in your project).
For your problem, where you want to add a border to a WebBrowser control. I would recommend creating a UserControl as per this tutorial.
Related
I am trying to create a new Control (Let us for now forget about UserControl or CustomControl).
I open a Windows phone Application project in vs2010 and then add a new item Windows phone Control Library and then I use it in my phone application.
Everything works fine. Now if I want to create a windows phone control to be used in another application how should I be going about it? New application means a separate new solution.
Phone Application1 -> Have my own control inside the solution and hence I am able to use it.
How can I use this same control in another new solution?
I am not sure if this has been answered before. Does any solution already exist?
Cavet:
I tried creating a new Windows phone Class Library in a vs2010 instance but it didn't work because it didn't had a xaml form. When I tried to manually include it and write the logic it build successfully but now even if I include the dll of this project I don't get the control in the toolbox. This process does not make much sense to me as I am creating a class library and not a control but still wanted to give it a try.
To show controls from your assembly in the toolbox, you should do design-time assembly. You can look at these articles for the beginning:
Link2
Link1
It's not easy and perhaps you can live without that if your control assembly is not a commercial product. To use any control from other assembly in application:
add project reference to your control assembly
include xmlns namespace attribute corresponding to your assembly to any page of your application. Just start typing 'xmlns=' near other xmlns definitions in the page header and VS intellisense will show you the list of available namespaces. Choose the needed one and then set namespace alias to use on the page. It should look something like xmlns:myNamespace="clr-namespace:MyAssembly.Namespace.;assembly=MyAssembly"
insert control from your assembly into page using xaml editor
now you should be able to see your control in xaml designer and edit its properties from the property grid
.
I am not sure if the above mentioned method would solve the case, because as far as I have seen it is not possible to create a Windows Phone Control Library in VS2010. So this is what I did..
Created a Windows Phone Control Library and write my own custom control.
Build the project.
Back to Vs2010, added the reference to the control in the app in which I wish to use.
As simple as that. I don't know why it was initially a little bit confusing (perhaps I would have got confused with the binding and stuff). Anyways its quite simple though. Thank you for the reply.
I'd like to get ahead of the pack and start making some custom C# controls and components for Metro (Win8), but I can't find any documentation or blog posts on how to start, or even if it's possible right now.
Are metro controls just WPF controls? I'm not yet a WPF developer; creating a custom user control looks straightforward, but that project type doesn't exist in the Windows 8 developer preview. So, are WPF custom user controls (VS2010) the project type I should use for creating my Metro controls? Beyond that, I can't find any documentation on how to create a component for use in WPF/Metro; can you create one, or would it just be a custom user control that isn't visible? (I was hoping for some type of component container like the one winforms use; are components for Metro now only class libraries, and don't include designer support anymore?)
It's ... very difficult right now. There's no way to override OnRender or the like for a control, ie to create code to render a control with a custom appearance. However, you can create a custom template. Anything you can do in the template is legal, and that's how you have to approach custom controls.
One other option is to generate your UI using either raster (Bitmap) or vector (Windows.UI.Xaml.Shape?) components directly and build up your UI like that. Bleh.
Win8 Metro is a lot like WPF, but it isn't a subset. Similar but very different. Many WPF controls didn't make the transition to Win8 Metro; the same level of rendering control isn't available; and some system features (like advanced font rendering) aren't there. All this might change in coming releases, but right now it looks like Microsoft is trying to restrain developers from creating custom UI controls.
I was wondering how could I save a user control just like we can save a c# class in a dll file and import that file whenever we need to use that class. How could we import that control and use it. It will be nice if we could make custom controls and save them for reuse instead of having to copy the xaml and code behind every time we had to use it.
You have exactly that flexibility with WPF and a lot of choices. Here is an excellent introduction to your options:
Control Authoring Overview
These choices include:
Deriving from UserControl
Deriving from Control
Deriving from FrameworkElement
The tradeoffs are described in detail in the article.
I need the DocumentViewer WPF control (for XPS files) in a Windows Form application. This is actually simple, but I'm not allowed to link in WPF assemblies. My boss' reasoning is that people will start using WPF controls which are incompatible with some of our software. I can't change any of this; it has to be XPS and cannot bring in more than one WPF control. I can use this control if I had a way to link it and only it (no other WPF controls/classes) to our software.
Is it possible for me to write some sort of wrapper on the control in a WPF Class library, and then import that DLL but the DLL does not allow access to other WPF controls? Is there a way for me to extract just that WPF control from the Presentation Framework assembly (probably not, and this sounds like something that violates the EULA)?
Or does anyone have a different approach they would like to suggest?
And no, not PDF please.
Just make a class library that does nothing but exposes a (Windows Forms) UserControl. Inside of this UserControl, put your ElementHost with the DocumentViewer. This way, you'll only load your project's UserControl, and the "WPF Controls" are isolated in the separate project.
This question already has answers here:
What is the difference between a User Control Library and a Custom Control Library?
(3 answers)
Closed 1 year ago.
I'm working on creating a date/time user control in WPF using C# 2008. My first user control. I'm also using Matthew MacDonald's book, "Pro WPF in C# 2008". In that book he strongly recommended creating a user control using the WPF Custom Control Library project template; so I followed his suggestion. I've finished writing the code which would go into what I think of as the code-behind file. Now I'm ready to write the XAML.
The only problem is, I just discovered there is no corresponding .xaml file? So, I don't get why using a WPF Custom Control Library project is better, or prefered, when writing a user control?
A user control and a custom control solve two distinctly different problems.
UserControls are meant to compose multiple WPF controls together, in order to make a set of functionality built out of other controls. This is often used to compose a portion of a window or screen in order to organize your development by allowing you to group multiple pieces of functionality into one "control". For example, if you wanted to make a control for editing a User which provided text boxes for first and last name, age, etc., a single UserControl could be dropped onto a Window and bound to a User instance to edit this. (In this case, you're using standard controls, such as TextBox, to "compose" a control for a more complex purpose.)
A CustomControl, however, is meant to be a new single control. This would typically be a replacement for a built-in control (which could not be redone via templating). I've found that the need for CustomControls is actually fairly rare in WPF, since the WPF templating options and attached properties allow you to do nearly anything with standard controls, once you learn them fully.
I would also add, if you're intending to inherit from your control, then using a usercontrol is will complicate matters. For example, if create a base usercontrol that has a layout defined in Xaml, WPF framework won't allow you to inherit this control and use Xaml to define the layout for the subclass. In dotnet 3.5 Xaml control can't inherit from another Xaml control