I am developing a Windows Store project, and I wish to move MainPage.xaml into a folder called Views. After that I want to rename it. I already attempted doing this, but I ended up breaking it (InitializeComponent could not find a definition).
How can I move and rename the page properly?
Actually, it seems that the MainPage type is hard-coded in the auto-generated code.
look at this post to get information on how to change it.
Let's say you renamed it to MyView.xaml, and moved it to Views folder.
You will probably want (not necessary) to also:
1. add ".Views" to the namespace in MyView.xaml.cs
2. add ".Views." to the x:Class tag in MyView.xaml
Now open App.cs, and locate the following line
if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
{
....
}
change MainPage to reference your new page name.
Simply make sure to keep the namespaces between the code behind and the xaml synchronized:
MainPage.xaml.cs:
namespace YourNameSpace.Views
{
public partial class MainPage : UserControl
{
}
}
MainPage.xaml
<UserControl x:Class="YourNameSpace.Views.MainPage"
....>
</UserControl>
Quick note: It's a little different on Windows Phone: the startup page is defined in the DefaultTask section of the WMAppManifest.xml file:
<Tasks>
<DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/>
</Tasks>
Related
I created a new blank Xamarin PCL app in Visual Studio 2015. It runs on all the platforms. I then add a XAML page named GuyBarSceneTabs to my PCL and change my app.cs file in my PCL so the App() code only contains
MainPage = new GuyBarSceneTabs();
This all works and I can run the projects again and it shows my XAML page.
Next I add another XAML page name NearbyLocations
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GuyBarScene.NearbyLocations"
Title= "Nearby Locations">
</ContentPage>
I also then change the GuyBarSceneTabs page to be a tabbed page as follows and also change the code behind page to inherit from TabbedPage:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:GuyBarScene;assembly=GuyBarScene"
x:Class="GuyBarScene.GuyBarSceneTabs">
<TabbedPage.Children>
<local:NearbyLocations />
</TabbedPage.Children>
</TabbedPage>
</TabbedPage.Children>
public partial class GuyBarSceneTabs : TabbedPage
{
public GuyBarSceneTabs()
{
InitializeComponent();
}
}
Now when I try to run the app I get the error message: "The given key was not present in the dictionary. There error is generated in the following code which is called from the InitializeComponent method of GuyBarSceneTabs page:
public partial class GuyBarSceneTabs : global::Xamarin.Forms.TabbedPage {
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private void InitializeComponent() {
this.LoadFromXaml(typeof(GuyBarSceneTabs));
}
}
Any suggestions as to what I am doing wrong? Do I need to change my App() code in some manner?
My story is similar but has a different solution so I thought I'd add it here for posterity. I had an existing View which received a ViewModel as a parameter. When I included it as one of the I got the "The given Key was not present" error. Upon rewriting the View without passing in parameters, then it worked.
So I figured this out. This problem appears to manifest if you rename a XAML and corresponding cs file in your project. It appears something gets messed up in the project file. The easiest thing to do is add a new XAML page with a new name and copy things from the old XAML file to the new file. Then it appears to work. In my case, the problem was caused by renaming the XAML page the inherited from TabbedPage file. Hope this helps anyone else who has this problem.
FYI the problem can also manifest itself if you include a XAML page and cs file from an other project and them rename them. In this case, when the project file gets messed up you will get an error that the InitializeComponent method does not exist will be thrown.
I have to say I REALLY want Xamarin to work well. I hope now that it is owned by MS it becomes more of a reliable, first tier development solution.
In my case key of the problem - default parameterless constructor was modified (added parameter).
After default constructor was added again - compilation error gone.
in my case i was making custom map control and write new constructor which is different from the default one to fix this issue i had to have both constructors
public SAMap()
{
}
public SAMap(MapSpan region)
{
}
I have an WPF solution and this solution consist of 3 project:
1-A project that has several WPF user control inside
2-Another project that has several WPF user control inside
3-A project which has Resources for 2 WPF projects above.
As you know, if you have common settings for you views like that
-Using Same FontFamily.
-Using same FontSize
-Using same FontWeight
-Using same BackroundBrush for all your User Controls etc.. You need to declare this setters in you all usercontrol tags like below:
<UserControl ....
FontFamily="{DynamicResource MyFontFamily}"
FontSize="{DynamicResource MyFontSize}"
FontWeight="{DynamicResource MyFontWeight}"
Background="{DynamicResource MyAppBgBrush2}"
Width="250" d:DesignHeight="350">
<Grid/>......
But I dont want to write same setters in all my UserControls. For thi reason, I decided to move this property setting in to a new c# file and locate it in Resource Project.
using System.Windows.Controls;
namespace Resources
{
public class PageBase : UserControl
{
public PageBase()
{
SetResourceReference(FontFamilyProperty, "MyFontFamily");
SetResourceReference(FontSizeProperty, "MyFontSize");
SetResourceReference(FontWeightProperty, "MyFontWeight");
SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
}
}
}
So, In my Resource project, I adited AssemlyInfo.cs file like this:
[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]
This edit gives me ability to declare/create a user control like below:
<internalresources:PageBase
xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
<Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>
From now, I do not have to create a usercontrol view which its tags start with
<UserControl...., I can start with <internalresources:PageBase......
My Question is that, VisualStudio 2010 can show me Design of all my user control bu Expression blend can not. Interesting part is that both in VS and Blend, my project compiling without any error But when I try to open my views in blend it says:
-The namespace 'PageBase' does not exist in namespace "http://schemas.sat.com/winfx/2010/xaml/internalresources"
P.S: References are added properly to my Project and My project was suitable to open with blend.
Can we link up two partial classes to a single ascx page.
I have gone through some of the links but could not find a suitable solution for the same.
Suppose for example I am having files such as
Collection.ascx.cs and Collection.Search.cs
with Collection.ascx
I am having events like button click event, could it be placed in the partial class, as doing so would make the method unavailable.
My intention is to handle button click events and the dropdown change events in the partial class.
Yes, In WebApplication, It is a default nature for a .aspx (web page) and .ascx (user control).
These are by default comes with two .cs files, separating code in partial classes (filename.aspx.designer.cs and filename.aspx.cs with same partial class)
You can also add more .cs files to extend these partial class definition.
But in website, it will not work, for aspx or ascx.
Yes, the C# compiler rules for partial classes don't change because its ASP.NET. Just make sure your namespace and class name matches, the source file doesn't have to be a child of the ascx file in the Solution Explorer, but if you really want it to you can edit the project file and add the appropriate depends-on xml segment.
The answer locates at default.aspx 1st line:
Language="C#" CodeBehind="Default.aspx.cs" ...
will set every partial class working.
Instead,
Language="C#" CodeFile="Default.aspx.cs" ...
will lead partial class to wrong.
However, in vs2017 old-style-webForm you can only use CodeFile, so you have to try another way: just create a file ..\WebF8\App_Code\Class3.cs with content :
public static class Class3 {
public static int gg3=33;
}
then you can see Class3.gg3 in default.aspx.cs correctly.
Be careful, this Class3.cs file must be in \App_Code folder.
I'm trying to learn monotouch at the moment, and following the learn monotouch book by Mike BlueStein. Not a bad book, but it's slightly outdated since xcode 4 (i believe) and a newer version on monotouch has come out.
Anyways, in my project I have a controller and a xib file. I also have a custom view (e.g. myview : UIView), that overrides the draw method. I want to show my custom view next to or on top of the view defined in the xib file. How do I do this?
In the controller, If I override the LoadView method, and set the View to an instance of my custom view, then I can see it, but I loose everything defined in the xib file. If I try to add as a sub view, it does not appear at all.
What am I missing? If the question is not clear, please ask me, so I can clarify.
Cheers.
Follow the following steps to use a custom view in a XIB:
First, decorate the view with the RegisterAttribute:
[Register("MyView")]
public class MyView : UIView
{
}
and implement the following constructor:
public MyView(IntPtr handle) : base(handle) {}
This constructor is needed for when the runtime will try to recreate the view after it has been destroyed by a memory warning. After you have created your custom class:
Open the XIB in Xcode (always by double-clicking on it through MonoDevelop) and add a UIView where you want it.
In Xcode, set that UIView's class to MyView (or whichever name you passed to the RegisterAttribute):
Compile-Run.
EDIT:
Do not override LoadView for controllers that are loaded from a XIB. LoadView is meant to create the controller's view when that controller's view is not loaded from a XIB.
Thanks Dimitris, great answer.
For those who get confused like me, here is the simplist procedure to add and use a Xib file as a partial / subview:
Add a new Xib File in MonoDevelop (ie LoginView.xib)
Add a new (partial) Class, this will be the custom class (The Code-behind file let 's say) for the view. Give it any name "LoginView.cs")
Add the Attribute (RegisterAttribte) and the Constrctor as exaplain above by
Dimitris.
Double click the LoginView.xib to open it in XCode IB. Then Change the Custom Class Attribute of the xib file to point to your "code-behind file" (ie LoginView.cs)
Add any outlets or action if you need. MonoDevelop will generate a .designer file and attach it to your code-behind file, this is where you can see all your outlets and actions.
In your controllers where you want to add this view, load your .xib file as follows:
var views = NSBundle.MainBundle.LoadNib("LoginView", this, null);
LoginView loginView = Runtime.GetNSObject(views.ValueAt(0)) as LoginView;
mainLayout.AddSubview(loginView);
// where mainLoyout is the placeHolder in my main ViewController
These three lines are based on flexaddicted answer here
My ultimate goal is to create a UserControl in a class in my App_Code folder and render the html from that into a string.
The rending of HTML can be done using this example I believe:
How do I get the HTML output of a UserControl in .NET (C#)?
My Question:
I created my user control
public partial class Controls_MyUserControl : System.Web.UI.UserControl
I've registered it in my web.config
<add tagPrefix="UC" src="~/Controls/MyUserControl.ascx" tagName="MyUserControl" />
I can reference this on a page just fine
<UC:MyUserControl ID="MyUserControl1" runat="server" />
Yet when I try to create an object of this control type in a class (in my app_code folder) it doesn't allow me.
Controls_MyUserControl dummy = new Controls_MyUserControl();
The potentially strange thing is when I put this code into a code behind of a page, it works.
Any ideas on what I need to do to have the usercontrol to be able to be created in my class in the app_code folder?
My guess is, is that I need to reference the control in a "using" statement at the top, but I'm not sure what that'd be.
It's not in a namespace to my knowledge (at least there's no namespace, in the code behind of the actually user controls). Though I'm not sure if it inherits one from the System.Web.UI.UserControl.
Thanks in advance
Some workaround as posted by Scott Alen http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-app_code-folder.html
Suppose you have all your user controls in ~/UserControls/
Then on your code behind add the following;
using YourSpaceName.UserControls;
This will set reference to any user control in that location.
Note: if you are using a user control within another, make sure to create a folder for each of them. I have experiencing problems, specifically with VB where it would compile but give me a runtime error. Since I encounter that problem I create my user controls each in it's own folder.