Dynamic content in User Control [Metro app, xaml] - xaml

I'm approaching to Metro App development in this days, so please be gentle.
I have created a User Control - some buttons and textblocks- that is loaded in every page of my app. I want the texblocks to change dynamically depending on the page selected: for example one of the texblocks of the user control is the page title. How can I accomplish this?

you can go for a simple code by finding the children of the usercontrol you are adding.
xaml code
xmlns:newPage="using:TestApp"
add the page like this in say mainpage.xaml:
<newPage:TestPage x:Name="pageNew"></newPage:TestPage>
then from code behind of the mainpage ie mainpage.xaml.cs
public mainpage(){
InitializeComponent();
var newPageContent = pageNew.Content; //here content will give u the immidiete children of usercntrl
}
Now you can type cast like
(Grid)newPageContent = pageNew.content;
var TextBlockFirst = newPageContent.children[0];
and so on :) please check if the suggestion is helpful :)

Related

XAML Xamarin Forms navigation drawer sample code for iOS/Android

Does anyone know how a navigation drawer similar to the one in the Sonos app can be implemented? I have checked both Android and iOS versions of the app and see that the navigation drawer has been implemented in the same way for both platforms.
The two key things that I like and want to implement are:
the slide out drawer is below the navigation bar. When it slides out, the navigation bar is still visible
it appears as if it is the drawer that slides out, rather than the detail view moving to the right. I've noticed that the default master detail page slides out in a different way and it's not what we want.
Have a look at the images below so see I mean.
thanks
Although not technically a good practice, if you put a MasterDetailPage into a NavigationPage, it will slide out like in the above pictures. Here's how you do that:
In the App.cs constructor or your app's OnStart() method:
MainPage = new NavigationPage(new MyMasterDetailPage()) {
Title = "Your Title"
};
Create a new MasterDetailPage called MyMasterDetailPage.
In the constructor, add the following code:
Detail = new HomePage();
Master = new MenuPage()
{
Title = "Menu"
};
You then need to create a ContentPage for both HomePage and MenuPage.
One issue that you will run into if you use this method, is that if you don't call MyMasterDetailPage as the first page upon opening the app, the three horizontal bars on the NavigationBar won't appear, which will make it hard for users to tell there is a drawer. So if you need users to go to a login page or another page before your MasterDetailPage, you may want to find another implementation.

Binding a ScrollViewer from ViewModel to View

I Build a scrollViewer and its elements in my ViewModel, and it's built into a property FrameworkElement PageElement I rebuild the pageElement every time some event happens, I want to bind the PageElement to a real scrollViewer in the View so that whenever I change pageElement, it draws itself in it's view.
Let me give you a little armchair advice. I don't know the details of your project but the details in your question make me draw a few conclusions.
First, to have your view model create UI elements is not wrong. But it is really unusual. It sounds like you might be missing the concept of data template or data template selector.
Using a data template allows you to have a rich presentation of data that is built as the individual record is generated and rendered in a repeater or in a single content control.
Using a data template selector allows you to have various different presentations of data that using code-behind logic will switch between based on data or other criteria.
Ref on templates: http://blog.jerrynixon.com/2012/08/windows-8-beauty-tip-using.html
Second, to have your UI be re-generated as the result of an event being raised sounds like a short path to performance problems.
Every time you manually create elements and add them to the visual tree, you put your app at risk of binding lag while the layout is re-rendered. Run your app on an ARM and I bet you may already see it. Then again, a simplistic UI may not suffer from this general rule of thumb.
Because I do not know the event, I cannot presume it is frequently occurring. However, if it is frequently occurring, then even a simplistic UI will suffer from this.
Now to answer your question
Sherif, there is no write-enabled property on a scrollviewer that will set the horizontal or vertical offset. The only way to set the offset of a scrollviewer is to call changeview().
var s = new ScrollViewer();
s.ChangeView(0, 100, 0);
You cannot bind to a method, so binding to something like this is a non-starter without some code-behind to read the desired offset and calling the method directly.
Something like this:
public sealed partial class MainPage : Page
{
MyViewModel _Vm = new MyViewModel();
ScrollViewer _S = new ScrollViewer();
public MainPage()
{
this.InitializeComponent();
this._Vm.PropertyChanged += (s, e) =>
{
if (e.PropertyName.Equals("Offset"))
_S.ChangeView(0, _Vm.Offset, 0);
};
}
}
public class MyViewModel : INotifyPropertyChanged
{
private int _Offset;
public int Offset
{
get { return _Offset; }
set
{
_Offset = value;
PropertyChanged(this, new PropertyChangedEventArgs("Offset"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
But let me caution you. The offset will need to be based on something. And those variables may change based on the window size, the font size, scaling from transforms, and lots of other factors. The code above will work most of the time, but it will possible fail frequently on other devices.
So, what to do? My recommendation is that you code this in your code-behind, monitoring for whatever scenario you feel would require a scroll, and simply programmatically scroll it from bode-behind. Beware, though, programmatically scrolling a scrollviewer could make your UI confusing to the user.
You know your app. You will have to choose.
Best of luck!

Windows 8 appbar on selected item (like startup screen)

What I want to achieve in my application is the same as what windows did on their start up screen. When you select an item, open the appbar and show more items. When you just open te appbar when not selecting anything, don't show those items.
So far so good and it works like I want it to be. But there is some issue I can't solve. When you select an item on the start up screen, you can't close te appbar when right clicking anywhere on the screen. You have to deselect the item.
I just can't figure out how to disable that right click to close the appbar. Because then I am in a state where I have a selected item, but no appbar. I don't want that...
Thanks in advance!
There is a property called IsSticky
this can be used to stop you appbar from collapsing.
the other option you have is to hooking into Closed event and force it to re-open.
Based on the screen you provided aka the Metro UI :) i'd say that create a page that just hosts other tiles.
I have done a similar thing in one of my apps, have a look at
http://apps.microsoft.com/windows/en-gb/app/cineworld/7adfde16-33c3-4b51-b758-00366325288d
Have a look at Coding4Fun or Callisto
Set the AppBar property IsSticky="true" and handle the event RightTapped on your GridView/ListView, and in the event handler set e.Handled = true;
This will stop the rightclick/swipe routed event from reaching Page and it won't close the AppBar.
You could override the hide() function of the appbar and replace it with something like this:
appBar.hide = function () {
/// <signature helpKeyword="WinJS.UI.AppBar.hide">
/// <summary locid="WinJS.UI.AppBar.hide">
/// Hides the AppBar, if visible, regardless of other state
/// </summary>
/// </signature>
// Just wrap the private one
var listView = document.getElementById("yourListView").winControl;
var count = listView.selection.count();
if(count <1)
this._hide();
}
It's not a great workaround, but it works like a charm :)

on(release) {...} or myButton.onRelease = function() {...} - action script 2 issues

I am having real confusion with some flash banners I'm creating and making the button into a clickable object which opens a web page.
I have been using this code for a while below, which works...
on(release){
getURL("http://www.the-dude.co.uk", "_blank");
}
And I placed this code on actual button within the actions panel
However I have been told the code above is very old and that I should use action script like below...
buttonInstance.onRelease = function() {
getURL("http://www.the-dude.co.uk", "_blank");
}
So I've tried to get this method below to work but nothing happens when I click the button, and I get one error, this...
So in a nutshell I cannot get this newer code to work! Ahh
Can anyone please help me understand where I am going wrong?
I have tried placing the new code in the Scene 1 of my actions. No worky..
And I've also tried placing the code below, actually on my button within the actions panel...
this.onRelease = function() {
getURL("http://www.the-dude.co.uk", "_blank");
}
Still not working.
My scene settings are alway this...
Any help would be great thanks.
You need to place the code below on the same timeline as the instance of the button (as you tried). And the instancename of the button must be "buttonInstance".
You can set the instance name in the properties panel when the button is selected.
buttonInstance.onRelease = function() {
getURL("http://www.the-dude.co.uk", "_blank");
}

Change applicationbar buttonicon at runtime

I'm developing a WP7 app and the application needs to change the icon of a button on the application bar given the state of a request.
I have tried:
if (App.Servers[index].ServerState == "Enabled")
{
DetailsAppBar.btnStart.IconUri = new Uri("/AppBar/appbar.stop.rest.png");
}
else
{
DetailsAppBar.btnStart.IconUri = new Uri("/AppBar/appbar.transport.play.rest.png");
}
This doesn't give me an error in the code, but it can't compile....
any hints to do this is appreciated :)
thanks
ApplicationBar is a special control that does not behave as other Silverlight controls (see Peter Torr's post on the subject). One of the consequences is that names given in XAML to app bar buttons generate fields in code-behind that are always null.
I'm guessing that in your case the btnStart field in DetailsAppBar is set to null, and thus trying to set its IconUri property results in a NullReferenceException being thrown.
To access a button in an application bar, you must instead reference it by its zero-based index in the buttons list. For instance, the code below returns a reference to the third button in the app bar:
button = (IApplicationBarIconButton)ApplicationBar.Buttons[2];
figured it out...
((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IconUri = new Uri("/AppBar/appbar.stop.rest.png",UriKind.Relative);
did the trick :)