Add user Control in a page in Windows Phone - windows-phone-7.1

I have a windows phone application and I need to add a user control to one of its pages. I want to add it like what in an asp.net page, not as a popup. How can I add this user control to the page?

Assuming your UserControl is in the format of something like:
<UserControl x:Class="UserControlExample.NameReporter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<!-- Controls here -->
</UserControl>
which it should by default when you create a new UserControl and the code behind is similar to
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace UserControlExample
{
public partial class NameReporter : UserControl
{
public NameReporter()
{
InitializeComponent();
}
// your custom methods here
}
}
you should then be able to add it to the page by using code similar to
<Grid xmlns:src="clr-namespace:UserControlExample"
Background="White" Margin="0,50,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<src:NameReporter Grid.Row="0"/>
<src:NameReporter Grid.Row="1" Margin="0,15,0,0"/>
</Grid>
Just change the namespace after clr-namespace: and the control name after src:
You can put the xmlns: tag into the <phone:PhoneApplicationPage> tag instead to use the control throughout the form (rather than just in a grid) and you can change the src to whatever you wish to refer to it as.
If the UserControl is created properly, compiling the solution should mean it appears in your Toolbox for use too, so you could then just drag & drop.
See the reference for a fuller example.
References:
http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol%28v=VS.95%29.aspx

For Windows Phone 8 and Windows Phone 8.1, I was able to accomplish this by doing the following:
Create your user control. In this example, I created a few Rectangles to mimic the classic mobile menu button.
Build Solution. The project will update and you shall see your user control now in the Toolbox.
Simply drag your user control out of the toolbox and drop it in the XAML page where you wish to use your user control.

Related

Java Server Faces equivalent to Html.ActionLink

I am using NetBeans for a Web Application in Java EE.
I have one controller for an absence entity class (absenceController) and one controller for an employee entity class (employeeController).
I am able to get to the employee creation page via any page controlled by absenceController:
<h:commandLink action="#{absenceController.prepareCreate}"
value="#{bundle.ListAbsenceCreateLink}"/>
I am able to get to the absence creation page via any page controlled by employeeController:
<h:commandLink action="#{employeeController.prepareCreate}"
value="#{bundle.ViewEmployeeCreateLink}" />
When I want to set up a new absence instance, I need the employee to be set in advance so I am trying to reach the absence creation page from an employee view page (called Tasks.xhtml), sending the employee instance as a parameter.
In MVC .NET I would have used an Html.ActionLink a bit like this:
Html.ActionLink("Report Absence for employee"
, "Create"
, "Absence"
, new { employeeid = employee.Id } // <- I would only be able to send the id
, null)
In Java, this is the closest I've got:
<h:commandLink action="/absence/create"
value="#{bundle.TasksEmployeeCreateAbsenceLink}" >
<f:setPropertyActionListener target="#{absenceController.selected.employee}"
value="#{employeeController.selected}" />
</h:commandLink>
The folder structure is shown below:
I know the action tag is not correct, but is there some way to change it to get the desired result please?
One way of achieving what I needed was to use navigation rules. (As in the comments, there are better way of achieving this since JSF 2.x)
I added the following to faces-config.xml (within <application></application> section):
<navigation-rule>
<from-view-id>/employee/Tasks.xhtml</from-view-id>
<navigation-case>
<from-action>absence/create</from-action>
<from-outcome>absence/create</from-outcome>
<to-view-id>/absence/Create.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
I am using JSF 2.2, so all I needed to do was use the correct case in Create:
<h:commandLink action="/absence/Create"
value="#{bundle.TasksEmployeeCreateAbsenceLink}" >
<f:setPropertyActionListener target="#{absenceController.selected.employee}"
value="#{employeeController.selected}" />
</h:commandLink>

How to iterate over variable in param using JSF EL expression

I have a PrimeFaces <p:dataGrid> component that contains a variable number of panels. Each panel contains a number of derived components. I have a delete button contained inside each of these panels to allow for deletion. I also have an add button outside of the dataGrid. Instead of using immediate="true" on the buttons, I figured out how to set the required attribute of each component in each panel.
For instance:
required="#{empty param['vehicleGrid:0:btnDelete'] and empty param['btnAdd']}".
For every delete button in the dataGrid and the add button, ignore component validation.
This works if there is a panel inside of the dataGrid, but it only references the first one. I need to dynamically check every panel. Maybe instead of looking at it from the markup page, maybe I need to look at it in Java terms since param is a Map<String, String>.
Bind the delete button component to the view and use UIComponent#getClientId() instead.
<h:inputText ... required="#{empty param[deleteButton.clientId]}" />
...
<h:commandButton binding="#{deleteButton}" ... />
This way the proper client ID will be looked up in the parameter map and there's then no need to iterate over the parameter map.

In N2CMS MVC with Razor, how do you add the Control Panel and Zones?

If you were using N2CMS MVC with the ASP.NET view engine, you would using the following code to add the Control Panel and a Zone to the page.
<n2:SlidingCurtain ID="sc" runat="server">
<n2:ControlPanel runat="server" EnableEditInterfaceIntegration="false" />
</n2:SlidingCurtain>
<n2:DroppableZone ID="Zone2" ZoneName="Left" runat="server" />
Is this even possible using the Razor view engine to enable the drag-n-drop zones? If so what is the syntax?
I have tried:
#{ Html.DroppableZone("Left").Render(); }
#{ Html.RenderZone("Left"); }
The above code renders the zone, but I am not sure how to enable the control panel or how to invoke the drag-n-drop style zone editing.
Turns out I was close.
Adding the following to the _Layout.cshtml enabled the Control Panel.
#{ Html.ControlPanel().Render(); }
And using DroppableZone enabled a target for the drag-n-drop.
#{ Html.DroppableZone("Left").Render(); }
Add the appropriate namespace to make sure the extension methods are available.
#using N2.Web.Mvc.Html;

How to Implement the ICommand as DependencyProperty

I am using the implementation give at Bind TextBox on Enter-key press to handle the enter key for text box.
But I am using MVVm pattern for my application. Therefore I have defined the ICommand handler in my VieModel class. I want to bind it to view.
The sample application uses
InputBindingsManager.UpdatePropertySourceWhenEnterPressed="TextBox.Text"
and I want to use
InputBindingsManager.UpdatePropertySourceWhenEnterPressed="{Binding myCommandHandler}"
instead.
Can anybody suggest what modification are required in the code?
You can go for inputbindings for a textbox. in this you can bind a Icommand object to command property ok keybindings
Here is the sample code
<TextBox Text="{Binding Firstname, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding ValidationCommand}">
</KeyBinding>
</TextBox.InputBindings>
</TextBox>

Custom controls with ASP.NET MVC Razor

How can I use custom controls with ASPNET.MVC Razor?
I want to use a custom control on a Razor view. for instance:
<mycontrols:something>#Model.MyVar</mycontrols:something>
or
<mycontrols:something myattribute="#Model.MyVar" />
Please note that my goal is to use only few controls derived from MvcControl, only for trivial repetive ui stuffs.
I tried to find out a syntax similar to #Register to write on the top of the view, but without any success.
Then I went to the web.config, adding
<pages>
<controls>
<add tagPrefix="mycontrols" namespace="mynamespace" assembly="myassembly"/>
</controls>
</pages>
but it looks like custom controls are ignored in rendering.
Someone could help?
...Might be it is a little bit old fashion, but sometimes also custom control could be useful to make your code cleaner!
The Razor syntax does not support the notion of Controls at all. If you want to use controls you will have to use the ASPX (WebForms) syntax.
However, the recomended MVC pattern is to use html helper functions or partial views. In Razor you can also use the #helper syntax for quick helper functions.
In ASP.NET MVC custom server controls should be avoided. Most of them rely on ViewState and PostBack which are notions that no longer exist in MVC. You should prefer using templates, HTML helpers to implement some reusable functionality. Another problem with controls is most of them encapsulate some business logic which fetches data from somewhere and renders it which is an anti-MVC pattern. In MVC it is the controller responsibility to manipulate the model and fetch data and pass a view model to the view which simply should display it.
MVC uses partial views rather than custom controls, and they can be used in two ways that cover pretty much everything a custom control can do
RenderPartial which renders data already retrieved by the page controller
RenderAction which is similar but has its own controller action so can get data independently
The only scenario I can think of where it would be worth putting a custom control on an mvc view is if you are working on a partially migrated webforms project, and I doubt that would work with anything other than the WebFormsViewEngine.
You can do this, though I don't recommend it, though I'm not here to judge. I might add that I don't expect postback and view state to continue working either but you can at least render all of the html.
The only way to do this is a bit of a hack, because razor doesn't support webforms view engine controls. However, it does support partial views made in the webforms View engine. So a hacky solution is to include a partial view with your control in it as such:
For example, say you want to use the office web ui ribbon in your mvc3 project, you could do so by including
<body>
<div>
#Html.Partial("_RibbonPartial")
</div>
</body>
in your view. Where _Ribbon is of type .aspx
then in your partial view simply use your control and set runat="server" and put it inside of a form with runat="server"
//_Ribbon.aspx
<form id="form1" runat="server">
<XyzControls:Manager ID="Manager1" runat="server" UITheme="Silver" />
<XyzControls:OfficeRibbon ID="OfficeRibbon1" runat="server" ApplicationMenuColor="#267c29"
ApplicationMenuText="Item" ApplicationMenuType="Backstage">
//... rest of control code
</form>
Then you need to use ajax to implement events instead of using postback.
Then to cleanup, get rid of the generated code from webforms view engine for post back that you don't need.. You can try keeping it, I haven't so I'm not sure what will happen. I know there are ways to have a fake viewstate as well if you really want to get into messy hacky stuff, but to remove the extra code from webforms you can use the following jquery:
$(function ()
{
// we don't need any of the webforms stuff
$("#__EVENTTARGET","#aspnetForm").parents("div:first").remove();
$("#__EVENTVALIDATION","#aspnetForm").parents("div:first").remove();
});
I had the same demand. Wanted to use custom webcontrol from Razor/MVC page. One should not do that with controls, that is handling postback. You don't have the eventcycle to support that, but if your only demand is to use a 'rendering control', you could instantiate it in razor, and control where the rendering takes place:
#{
var myControl = new mycontrols.something();
myControl.myattribute = Model.MyVar;
mycontrol.RenderControl(new HtmlTextWriter(this.Output));
}

Resources