How wide is the master page of a Master-Detail Page in Xamarin.Forms? - layout

Depending on the screen size (and device idiom?) the width of the master page varies: On phones it is about 80 % of the screen width, while on tablets it seems to be a constant dimension like 320 dp.
Does anybody know a general formula for this value? I'd like to use it for laying out some elements during construction time, when the Width property isn't set, yet.
Edit:
I know how to get the current screen size. But how does the width of the presented master page of Xamarin.Form's master-detail page relate to it? It doesn't cover the whole screen, but fills a different fraction of it depending on the device.

You could request the device's actual screen width, height, and scale factor.
(From https://github.com/mattregul/Xamarin_GetDeviceScreensize)
iOS AppDelegate.cs
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth = (double)UIScreen.MainScreen.Bounds.Width;
App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height;
App.DisplayScaleFactor = (double)UIScreen.MainScreen.Scale;
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
Android MainActivity.cs
[Activity(Label = "Xamarin_GetDeviceScreensize.Droid", Icon = "#drawable/icon", Theme = "#style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density; // Width = WidthPixels / Density
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density; // Height = HeightPixels / Density
App.DisplayScaleFactor = (double)Resources.DisplayMetrics.Density;
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
Xamarin Forms App.cs
public class App : Application
{
public static double DisplayScreenWidth;
public static double DisplayScreenHeight;
public static double DisplayScaleFactor;
public App()
{
string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" +
$"Width: {DisplayScreenWidth}\n" +
$"Height: {DisplayScreenHeight}\n" +
$"Scale Factor: {DisplayScaleFactor}";
// The root page of your application
var content = new ContentPage
{
Title = "Xamarin_GetDeviceScreensize",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
Text = ScreenDetails
}
}
}
};
MainPage = new NavigationPage(content);
}
}

Related

rendering svg in xamarin form resulting in black square

I am trying to print svg pictures on my xamarin forms app.
I am using SkiaSharp.
I implemented a component using a Bindable property to get the byte[] of the svg and I draw it in the component.
public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(Image), typeof(byte[]), typeof(SvgCanvas));
public byte[] Image
{
get => (byte[])GetValue(ImageProperty);
set
{
if (Image == value)
{
return;
}
SetValue(ImageProperty, value);
CanvasView.InvalidateSurface();
}
}
private void OnPainting(object sender, SKPaintSurfaceEventArgs args)
{
SKSurface surface = args.Surface;
SKCanvas canvas = surface.Canvas;
int width = args.Info.Width;
int height = args.Info.Height;
// clear the surface
canvas.Clear(SKColors.White);
// the page is not visible yet
if (Image == null)
{
return;
}
SKSvg svg = new SKSvg(new SKSize(width, height));
using (MemoryStream ms = new MemoryStream(Image))
{
svg.Load(ms);
}
// draw the svg
// canvas.DrawPicture(svg.Picture, ref matrix);
canvas.DrawPicture(svg.Picture);
}
It looks like the Picture is loaded but only a black square is displayed on my view...
My component is called like this in my page: (Picture is a byte[])
<controls:SvgCanvas Image="{Binding Picture}" />
Did somebody had the same issue? Maybe an encoding problem?
I am kind of lost..

LibGdx: How to set a separate viewport for UI elements?

I'm trying to code a simple maze with a character that moves using a Joystick (touchpad). I used a Fit viewport to maintain the aspect ration of my maze while keeping it at the center of the screen;
However, I find that that the joystick is also added beside the maze. sample of what it looks like currently
I want the joystick to be of the far bottom-left of the screen. I tried doing this using a different viewport for UI elements (including the joystick), but then the whole maze just gets distorted.
Here is part of my BaseScreen class, an extension I made to reuse in other games as well:
public abstract class BaseScreen implements Screen, InputProcessor {
public BaseScreen(){
gameWorldHeight = 1024;
gameWorldWidth = 1024;
cam = new OrthographicCamera(gameWorldWidth, gameWorldHeight);
port = new FitViewport(cam.viewportWidth, cam.viewportHeight, cam);
port.apply();
cam.update();
mainStage = new Stage(port);
uiStage = new Stage(port);
uiTable = new Table();
uiTable.setFillParent(true);
uiStage.addActor(uiTable);
initialize();
}
public abstract void initialize();
public void render(float dt) {
mainStage.getCamera().update();
uiStage.getCamera().update();
uiStage.act(dt);
mainStage.act(dt);
update(dt);
mainStage.draw();
uiStage.draw();
}
public abstract void update(float dt);
public void resize(int width, int height) {
mainStage.getViewport().update(width,height, true);
uiStage.getViewport().update(width,height,false);
}
}
Here is also a part of my gameScreen class, where I actually code all the core mechanics of the game
public class gameScreen extends BaseScreen {
#Override
public void initialize() {
//..
uiTable.pad(10);
uiTable.add().expandX().expandY();
uiTable.row();
uiTable.add(ball.touchpad).left();
uiTable.add().expandX();
}
}
and in case you were wondering, here is what ball.touchpad is
public class Ball extends BaseActor {
public Touchpad touchpad;
public Touchpad.TouchpadStyle touchpadStyle;
public Skin touchpadSkin;
public Drawable touchBackground;
public Drawable touchKnob;
public Ball (float x, float y, Stage s) {
//..
//Create a touchpad skin
touchpadSkin = new Skin();
//Set background image
touchpadSkin.add("touchBackground", new Texture("touchBackground.png"));
//Set knob image
touchpadSkin.add("touchKnob", new Texture("touchKnob.png"));
//Create TouchPad Style
touchpadStyle = new Touchpad.TouchpadStyle();
//Create Drawable's from TouchPad skin
touchBackground = touchpadSkin.getDrawable("touchBackground");
touchKnob = touchpadSkin.getDrawable("touchKnob");
//Apply the Drawables to the TouchPad Style
touchpadStyle.background = touchBackground;
touchpadStyle.knob = touchKnob;
//Create new TouchPad with the created style
touchpad = new Touchpad(10, touchpadStyle);
//setBounds(x,y,width,height)
touchpad.setBounds(15, 15, 200, 200);
}
So how can i add this touchpad on the bottom left of the screen?

How to bind Map/Image in a List with Click event?

I want to bind map with a field and its click event as well, which will take it to a MvxCommand and show some MapViewModel.
[Register("HoursEntryCell")]
public class HoursEntryCell : MvxTableViewCell
{
public HoursEntryCell()
{
CreateLayout();
InitializeBindings();
}
public HoursEntryCell(IntPtr handle)
: base(handle)
{
CreateLayout();
InitializeBindings();
}
private UILabel hours;
private UIImageView imageView;
private UILabel jobName;
private MKMapView location;
private void CreateLayout()
{
jobName = new UILabel(new RectangleF(10, 10, 100, 30));
jobName.AdjustsFontSizeToFitWidth = true;
jobName.Lines = 0;
jobName.Font = jobName.Font.WithSize(16);
imageView = new UIImageView(UIImage.FromBundle("pencil.png"));
imageView.Frame = new RectangleF(270, 10,imageView.Image.CGImage.Width, imageView.Image.CGImage.Height);
Accessory = UITableViewCellAccessory.DisclosureIndicator;
location = new MKMapView(new RectangleF(15, 40, 280, 160));
location.AddAnnotation(new MKPointAnnotation()
{
Title = "My Loc",
Coordinate = new CLLocationCoordinate2D(23.0092509, 72.5061084)
});
location.UserInteractionEnabled = false;
salaryLable.Text = "Salary";
hours = new UILabel(new RectangleF(200,200,50,50));
ContentView.AddSubviews(jobName, location, hours,salaryLable, imageView);
}
private void InitializeBindings()
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<HoursEntryCell, ListViewModel>();
set.Bind(location).To(vm => vm.MyLocation);
set.Bind(hours).To(vm => vm.Salary);
set.Bind(jobName).To(vm => vm.EmployeeName);
set.Apply();
});
}
}
}
I want to achieve something like set.Bind(location).To(vm => vm.GoNextCommand); along with the map (set.Bind(location).To(vm => vm.MyLocation);)
Or How can I bind simple image button click event to a MvxCommand from the list?
How can I go for it?
Need Help.
You'll probably need to do a combination of things to get this to work properly...
1.) Do your binding from the view that has your list view. In the example above how does the cell actually get access to your view model? Look at the example here: https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/DailyDilbert/DailyDilbert.Touch/Views/ListView.cs#L16
2.) you'll need to probably create custom bindings to handle the map view or it might be similar to this example from Stuart: MvvmCross iOS: How to bind MapView Annotation to jump to another view?

Converting ToolBar to ToolStrip control and MouseHover not working

I have a large winform application which we working to modify the appearance. I am replacing System.Windows.Forms.Toolbar to System.Windows.Forms.ToolStrip control. I use a custom renderer to change dropdown arrow color. with default renderer i get mouse hover effects in toolstrip but with my custom rendering it dont seem to work. Here's my code.
Tool strip initialization:I removed unnecessary code for reading comfort
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.toolStripDropDownButton1 = new System.Windows.Forms.ToolStripDropDownButton();
this.toolStrip1.ImageList = this.imageList1;
this.toolStrip1.ImageScalingSize = new System.Drawing.Size(55, 32);
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripDropDownButton1
});
this.toolStrip1.Renderer = new MyRenderer();
Toolstrip dropdown button:
this.toolStripDropDownButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripDropDownButton1.ImageIndex = 0;
this.toolStripDropDownButton1.Name = "toolStripDropDownButton1";
CustomRenderer
public class MyRenderer : ToolStripRenderer
{
protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
e.ArrowColor = Color.White;
base.OnRenderArrow(e);
}
}
thanks to #LarsTech for his help. I found this working. I made this below modification in renderer and in code.
Added this line in initialization
this.Toolstip1.RenderMode = ToolStripRenderMode.Professional;
CustomRenderer
public class MyRenderer : ToolStripProfessionalRenderer //Professional renderer
{
protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
e.ArrowColor = Color.White;
base.OnRenderArrow(e);
}
}

Section.HeaderView is resizing automatically in Monotouch Dialog

I'm using Monotouch.Dialog and
I have subclassed the class Section to implement a custom HeaderView. The problem is that the header view is automatically resized, here's the code I'm using :
public class MySection : Section
{
public MySection ()
{
UIView v = new UIView(new RectangleF(0,0,30,30));
v.BackgroundColor = UIColor.Red;
this.HeaderView = v;
}
}
I've attached the screen shot :
http://imageshack.us/photo/my-images/864/capturedcran20120508212.png
EDIT
Ok I've kind of find the way to resolve this
The trick is to add a View that will contain your real HeaderView and sets the AutosizesSubviews property to false. Then just add the view to the latest :
public class MySection : Section
{
public MySection ()
{
UIView v0 = new UIView(new RectangleF(0,0,50, 60));
v0.AutosizesSubviews = false;
UIView v = new UIView(new RectangleF(0,0,30,30));
v.BackgroundColor = UIColor.Red;
v0.AddSubview(v);
this.HeaderView = v0;
}
}

Resources