Creating view with json (Xamarin.Forms) - c#

I'm facing several issues.
I want to deserialize my json to create Views.
I want to create view, so I created a json model who describe my page with several views in it. I have created classes who's going to create my views. Before the json I was doing this in static:
ElementModel element = new ElementModel();
element.Width = 100;
element.Height = 40;
element.Top = 0;
element.Left = 0;
element.Label = "Element num 1";
element.IsEnabled = true;
element.IsMandatory = false;
ElementView ev1 = new TextBoxView(element);
al.Children.Add(ev3.getView());
this.Content = new ScrollView { Content = al };
And it worked fine, this code above was creating an Entry view placed with top, left informations and the width, height dimensions in the json.
I wanted something more dynamic so I created a json, I deserialize it and after that I create my views.
ScrollView sv;
RelativeLayout rl;
AbsoluteLayout al;
public MainPage()
{
InitializeComponent();
al = new AbsoluteLayout();
testDeserialize();
//I was creating view with this function before
//testView();
}
public void testDeserialize()
{
string json = #"myjson in the link";
string newjson = json.Replace("'", "\'");
TourApplication ta = JsonConvert.DeserializeObject<TourApplication>(newjson);
models.Page p = ta.Pages.ElementAt(0);
ElementView ev=null;
foreach (ElementModel em in p.Elments)
{
//ev = new TextBoxView(em);
//al.Children.Add(ev.getView()); doesn't work
Console.WriteLine("-----------------------"+em.Label);
}
this.Content = new ScrollView { Content = al };
}
The creation doesn't work but I'm doing the same operation when it was working.
I have no idea what I can do to fix this.
You can find in this link the json with the generated class structure.
Thx for your help.
EDIT : The problem was due to my json, now I can have my Views. But only one of my View is editable. I mean for the others the keyboard doesn't show.

Related

Dynamically Generate Groupboxes

I'm working on an inventory program and have finished the main functionality as a command line console app. I am now working on a version for winforms. I want to enable it to dynamically generate a Groupbox that holds some textboxes. I'd rather not design 50+ lines of multiple textboxes. Keep in mind I'm rather new to programming, having started with C# a year ago. I know next to nothing on Winforms.
I've tried to use dynamic item = new Groupbox();as a similar method allowed generation of objects at runtime. In the command line app, the way it works is that based on information given, a certain amount of objects are passed into the list _AllItems. I was thinking of generating the Groupboxes by using:
private void InitializeGroupBox()
{
foreach (Product product in Product._AllItems)
{
dynamic Item = new GroupBox();
}
}
But I have the feeling I'm nowhere near the correct method. Thanks to anybody who helps.
You will need to learn a bit more, but here is what I usually do to achieve what you asked.
internal class DynamicForm : Form
{
private FlowLayoutPanel mFlowLayoutPanel;
public DynamicForm()
{
mFlowLayoutPanel = new FlowLayoutPanel();
mFlowLayoutPanel.Dock = DockStyle.Fill;
// Add to this Form
this.Controls.Add(mFlowLayoutPanel);
InitializeGroupBox();
}
private void InitializeGroupBox()
{
mFlowLayoutPanel.SuspendLayout(); // Performance
for (int i = 1; i <= 20; i++) {
var groupBox = new GroupBox();
groupBox.Text = "GroupBox #" + i;
groupBox.Size = new Size(200, 50);
var textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
// Add the TextBox to GroupBox
groupBox.Controls.Add(textBox);
// Add to this Form
mFlowLayoutPanel.Controls.Add(groupBox);
}
mFlowLayoutPanel.ResumeLayout(); // after suspend, resume!
}
}

Binding a HTML control into a stacklayout using xamarin forms

Being a newbie to Xamrin I am struggling to adding some HTML to a StackLayout via Xamarin Forms. I have tried quite a few things and had a Google around.
Firstly I can't work out which bindable object I am supposed to be using. As I cannot find a straight answer on Google/Xamarin I am going to assume this is not as easy I was hoping.
var nameEntry = new Label ();
nameEntry.SetBinding (Label.TextProperty, "Club.ClubName");
var webView = new WebView ();
webView.SetBinding ( ??? , "Club.Description");
var content = new StackLayout {
Children = {
nameEntry,
???
}
};
I am not sure if this is possible within Xamarin forms itself. Can anyone help?
I should point out my data for the form is being retrieved asynchronously on a remote json endpoint
protected override void OnAppearing ()
{
base.OnAppearing ();
if (ViewModel == null || ViewModel.IsLoading)
return;
ViewModel.LoadItemsCommand.Execute (Club.ClubId);
}
My remote json api contains, Description contatins a HTML snippet which I would like to use.
{
ClubName: "Stourbridge",
Description: "<p>This club meets every TWO weeks on a <b>Friday</b>.</p>"
...
}
Try the following example that will show how to do the bindings.
Note that you have to use a HtmlWebViewSource to achieve this, and bind the WebView.Source to this.
Clicking the button will change the view model and update the WebView appropriately to the newly changed text.
StackLayout objStackLayout = new StackLayout();
MyView objMyView = new MyView();
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text</p></body></html>";
HtmlWebViewSource objHtmlWebViewSource = new HtmlWebViewSource();
objHtmlWebViewSource.SetBinding(HtmlWebViewSource.HtmlProperty, "MyHtml");
objHtmlWebViewSource.BindingContext = objMyView;
WebView objWebview = new WebView();
objWebview.HorizontalOptions = LayoutOptions.FillAndExpand;
objWebview.VerticalOptions = LayoutOptions.FillAndExpand;
objWebview.Source = objHtmlWebViewSource;
Button objMyButton2 = new Button();
objMyButton2.Text="Change Html";
objMyButton2.Clicked+=((o2,e2)=>
{
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text that has changed.</p></body></html>";
});
objStackLayout.Children.Add(objMyButton2);
objStackLayout.Children.Add(objWebview);
The view model is just a simple one with a bindable property as below:-
public class MyView
: Xamarin.Forms.View
{
public static readonly BindableProperty MyHtmlProperty = BindableProperty.Create<MyView, string>(p => p.MyHtml, default(string));
public string MyHtml
{
get { return (string)GetValue(MyHtmlProperty); }
set { SetValue(MyHtmlProperty, value); }
}
}
Before clicking the button gives:-
After clicking the button, adjusts the view model, and automatically updates the control via the binding giving:-

TableLayoutPanel: How can i navigate with tab key through dynamically added controls?

I have a form which contains a dynamically added TableLayoutPanel, which contains some dynamically added Labels, TextBox, CheckBox. I am obtaining exactly the visualization I would like to have, but I am struggling to get the "tab key" to work for moving from one control to the other.
I have tried to add a:
control.TabIndex = tabIndex++;
control.TabStop = true;
But this doesn't seem to have any impact...
This is the (tested) stub code:
class MyForm : Form
{
public MyForm()
{
InitializeComponent();
string[] titles = {"first","second"};
var myLayout = new TableLayoutPanel();
myLayout.AutoSize = true;
int myTabIndex = 1; //Not really necessary
int rowNumber = 0;
foreach (var title in titles)
{
var label = new Label();
label.Text = title;
myLayout.Controls.Add(label, 0, rowNumber);
var control = new TextBox();
control.TabIndex = myTabIndex++; //Not really necessary
myLayout.Controls.Add(control, 1, rowNumber);
rowNumber++;
}
this.Controls.Add(myLayout);
}
}
This is the window I get, and I am not able to navigate from first to second field using the tab key.
Update:
Applying Visual Studio 2013 Update 5 did not help.
Something must have been corrupted in my project. The best I could do is to move on with a new clean Windows Form project, and everything now is working.

WP8 loading listbox locations on map

I'm new to wp8 application development I would like to know how to load the map locations with listbox places.. I have no idea on it so please help me to know how to do it.. thanks in advance..
I have a listbox with the places loaded with its latitude and longitude and a MAP button a side.. when I click the MAP button I should display the listbox places into the map.. so please anyone help me to do this...
This is a code to just add map to the page.. (code behind)
private void Map_Click(object sender, RoutedEventArgs e)
{
Map MyMap = new Map();
MyMap.Height = 392;
MyMap.Width = 412;
MyMap.Center = new System.Device.Location.GeoCoordinate(-25.235,133.611);
MyMap.ZoomLevel = 2;
MyMap.LandmarksEnabled = true;
MyMap.PedestrianFeaturesEnabled = true;
MyMap.ColorMode = MapColorMode.Dark;
srch.Children.Add(MyMap);
}
Well I don't know how your listbox looks like so I just created a empty Pushpin for each location.
Following Method loops through every row in your ListBox and calls the method to draw the pushpin
private void ReadListBox()
{
foreach (var location in YourListBox)
{
DrawLocationToMap(new GeoCoordinate(latitudeInListBox, longitudeInListBox));
}
}
Following method creates the location pushpin and adds it to your map
private void DrawLocationToMap(GeoCoordinate currGeo)
{
Pushpin locationPushPin = new Pushpin();
locationPushPin.Background = new SolidColorBrush(Colors.Black);
MapOverlay locationPushPinOverlay = new MapOverlay();
locationPushPinOverlay.Content = locationPushPin;
locationPushPinOverlay.PositionOrigin = new Point(0.5, 0.5);
locationPushPinOverlay.GeoCoordinate = new GeoCoordinate(currGeo.Latitude, currGeo.Longitude);
MapLayer locationLayer = new MapLayer();
locationLayer.Add(locationPushPinOverlay);
MyMap.Layers.Add(locationLayer);
}
You'll need the WPToolkit to use PushPins.
To install the toolkit you'll need NuGet. How to install: http://docs.nuget.org/docs/start-here/installing-nuget
After NuGet is installed, open the console
and enter
Install-Package WPtoolkit
Finally just add the namespace into your xaml
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

Treeview is not inserting or showing data

I am using MonoDevelop to create a GUI but it doesn't contain a ListBox conrol. I read that Tree View is a good alternative so I'm trying to get it to work, but nothing appears to be added to the Tree View at all.
ListStore _store;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
_store = new Gtk.ListStore (typeof(string));
lbErrors.Model = _store; // lbErrors is the Tree View
Error("err");
}
public void Error(string err)
{
var ii = _store.Append();
_store.SetValues(ii, err);
}
Can anyone spot the problem?
You need to set your table columns.
Try something like this:
var column = new TreeViewColumn ();
column.Title = "Column Name";
column.Clickable = false;
var renderer = new CellRendererText ();
column.PackStart (renderer, true);
column.AddAttribute (renderer, "text", 0);
lbErrors.AppendColumn (column);

Categories

Resources