Short story : can you make 2 XAML to reference into single C# class? If so, how do you add the reference in the XAML?
Long story : I'm currently making an Universal App for 8.1 in VS. After investigating the code-behind, both platform have same codes (identical). So my plan is to put the class for code-behind in the Shared folder, but I still don't know how to make both XAML (Windows 8.1 and WP8.1) to refer to this class as code-behind.
I've read this one : How do you reference a class through xaml?
But this is for WPF, and it specified the class as static
edit : I'm not making the XAML into shared file, since I've designed them platform-specific
You are probably looking for the x:Class attribute of the Page element in XAML
However, it would be better to use two xaml.cs files for each xaml page in the windows store and windows phone projects, and have the common code in a separate class in the Shared Project.
Still better, you could keep all your code in the shared project, and use a single xaml page and single xaml.cs class for both phone and tablet, by using the VisualStateManager. This is what you will have to do when you port your app to Windows 10, which is a truly universal app.
You can probably just put the xaml.cs file in the shared project, or create a third file for the shared code and declare it as partial. Then implement the differences in the projects.
However, if you do this, I think you'll have problems with adding event handlers to the xaml, much as you do if you create a base class and derive from it, as the handlers will be added to the local project, not the shared one.
Try to use code-behind as less as possible. XAML is designed with MVVM in mind, so code-behind should be minimal, ideally empty. Besides MVVM, there're attached behaviors, custom controls etc. which help moving code from code-behind.
If you can't get rid of code-behind completely, use your usual tactics of sharing a common piece of code — just move the code into a separate class, for example, or use class hierarchy.
Note that besides XAML and code-behind, there's a generated file which connects control names to control fields etc., so there's more code than you see. Code-behind relies on this generated code, so it can't be shared within the same project, even if the code is the same.
Just some ideas (may not be perfect):
Add the UI of both controls into one XAML file and then just hide/remove the unnecessary one.
Use a template (custom) control.
Files in the shared project count as part of the other projects that reference the shared project. When you're building the configuration specific project it doesn't matter if a file is in that project directly or if it is in the shared project.
That means that you can leave the .xaml files in the target-specific projects and move the .xaml.cs file to the shared folder.
For Universal Windows apps in Windows 10 it's even easier: the controls will all be the same so you don't need separate code. If you do want to tailor your experience to different devices you don't need a shared project but can use device qualifiers to provide separate xaml files for different targets.
I discuss using partial classes and resource dictionaries to share code in my blog entry Strategies for sharing code in XAML-based Universal apps
Related
Good day, I want to port my Windows Forms App to WPF, I have been investigating and I know that it's almost impossible to port it because of the designer.cs files of Winforms and WPF uses .XAML, but, this web pageconverts your designer files to XAML, so, it is still possible?
You can't port YourForm.Designer.cs because the System.Windows.Forms and System.Windows.Controls are different classes. But there's still a way. Add the namespace of Windows Forms to XAML code. Then, use a WindowsFormsHost. You may add them only with WindowsFormsHost. Then, just copy your methods to C# code of WPF application.
Note: Do not forget to name your controls in WindowsFormsHost correctly.
Note 2: You must check your code after that operations. Because something may need to change.
I recently inherited a Xamarin project where I am to work on the iOS project. Going over several tutorial I figured I was ready, however the person before me did not use storyboards or controllers! They did all customization of views in files named LoginScreens.cs (basically controller files from the looks of it).
For learning purposes and ease of transition I would like to get a storyboard going in this project. So I created a storyboard titled Main and added a ViewController and essential copy and pasted the view customization code used in the LoginScreen.cs into my controller.
To be a little more specific, I have a Main.Storyboard that looks like this:
Where I am using editText boxes and a button to act as place holders for what I actually do to them in the ViewController.cs.
This all seems to register and builds properly however when I run the debugger on iPhone 6s iOS 9.3 I get the following:
The changes appear to show, but all my storyboard iOS designer views remain in place. I am trying to see if there is a way to reflect the changes made in the controller on the storyboard.
TL;DR: I'm trying to alter some views in a programmatic way in my ViewController.cs file. These views were originally added via the iOS designer and for customization purposes, they were edited in the controller. I want to see the visual alteration I make on a view in the controller, reflect in the iOS designer and when I debug.
Sounds like you are trying to go from a project where views were all done programmatically to implementing storyboards. This is a Big change and will take some time to convert the views over.
In the cs file there will be things like Add(passwordTextFeild) which are going to add more views to your storyboard view, hence why you get alot of views in your login view. You wont be able to see these in the storyboard as they are done at runtime.
If you are looking for IBDesignable this is more for custom controls and you still will have to add code to be able to see the changes from the .cs file in the storyboard.
Check my Question for IBInspectable/IBDesignable in Xamarin
Check this official link: https://developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_walkthrough/
Probably not what you wanted to hear but the UI in iOS projects tend to be done in one of the three methods:
All programmatically
Storyboards
Xibs
There are tons of questions/blog posts (even a video), weighting up the pros and cons of each. So possibly the last developer felt it was best to do it programmatically
There is a setting, IBDesignable, that you can add to the declaration of UI classes in Swift or Objective-C. That tells Xcode the those UI objects have a custom interface that you want to be presented in Interface Builder.
I have no idea how (or even if you could) you would use IBDesignable in Xamarin/C#. If its not supported for Xamarin then you're probably out of luck.
I suggest you search on "Xamarin IBDesignable" on the net.
In my Prism WPF modular application I need in the following common shared data: 1) Two strings with names of shell window' regions, 2) The ResourceDictionary with common style for radiobuttons (which are defined in modules). Below is the screenshot of my app window when it runs.
Below is the structure of modules in my application:
AuthorizationNavigationItemView.xaml and AuthorizationNavigationItemViewModel.cs are 'Athorization' radiobutton and its view model correspondingly. And CalibrationNavigationItemView.xaml and CalibrationNavigationItemViewModel.cs are 'Calibration' radiobutton and its view model correspondingly. Later on, the number of modules (and radiobuttons correspondingly) will reach up to 20 - 25 and all of them will need in common style placed in common ResourceDictionary. Besides this, all modules need in common resource - two strings that are the names of regions in the shell window: "MainNavigationRegion" and "MainContentRegion". Where should I place common ResourceDictionary and common string resource in my application? Your help will be appreciated highly.
It depend on what you gonna do with this Resource file. Usually I separate to independent project all string/text resources. It because my app need localization, translation, etc.. so this file will be reviewed/changed by other people. So goes Styles and Templates resources - this for designers. Please note that designer usually re-build the project/solution in Blend or other tools so this resource project should take minimal reference to other project (ideally it should not reference anything else)
So - if you work in a team and other people will be manipulating the text/string/styles/etc.. in you app I suggest you create separate, independent project so other people could easily change it. Take in consideration that this project could come very late in delivery process so you should create some generic/invariant resource and use it untill 'real' file come in
Let's say I have ten Apps. I also have a project with an "AboutPage" that downloads feeds from my blog. I don't want to recode or copy & paste that page and it's logic in every project of my Apps; also if I made some update to that page, I would like them to be visible to the other projects.
I think I would need to reference the page into the other projects, but I can I solve the problem of the different namespaces and so on?
Is a good idea making a custom control like a "MyBlogFeedReader" and put it into a page?
We can create a runtime component and move the xaml page into it. Then reference the runtime component into your different projects.
I have a similar runtime component SharedXAML and have a xaml page SharedPage defined in it. In other project, it's so easy to navigate to the page as below:
Frame.Navigate(typeof(SharedXAML.SharedPage));
I've created a win form application which consist of a single form. We have 8 tabs to access the modules of application.
The problem is we are a team of 4 who works on this project. But since it is a single form application, only one person can use the file at a time. Is there anyother way to build application with more than one file?
Please provide some solution.
Firstly, you should probably have a separate UserControl per tab. That will give you 8 files (at least) since you have 8 tabs.
Secondly, you should be using a Model-View-Controller style architecture for Windows Forms applications. That will give you at least one controller, but likely you will have one controller per UserControl (i.e. per tab). You might even have an overall controller that manages the per-tab controllers.
You might only have one data model for the entire app, or you might have one data model per UserControl (tab).
If you did all that, you'd have a few more source files.
However, it's actually difficult to say without knowing anything about your app.
Try using user controls to make each tab modular.
Figure out what are the parameters that each tab accepts and that it exposes and then create user controls that have that behavior.
Here are couple resources to get you started
http://msdn.microsoft.com/en-us/library/aa302342.aspx
User Control vs. Windows Form
User Controls in Windows Forms - Anything similar to ASP.NET User Controls?
Even if this is a giant ball of wax, your source control tools are shoddy and breaking it up into separate classes is hard to do, you can still take advantage of a Form class being a partial class. Which means that you can spread the code over any number of source code files, not just the two files that the designer creates. So a logical organization is to move code that belongs to a particular tab in its own partial class with the same form class name and its own source code file. Some cut+paste required however when you add event handlers with the designer.
Have you considered using MDI?
MSDN Working with MDI...
Examples are in VB.Net but I'm sure it will be easy to use C# if you really want to - I'm not sure why, but... :)