UWP - Inherit from Base Page C# - windows

I'm one of the many windows app developers. I'm sure someone other ran in this problem too. I want to have a base page (c#) where I add methods and use it in some pages without coding it over and over.
I've tried it like this:
Basicpage.cs:
public class BasicPage : Page
{
public void Test() {
}
}
SettingsPage.xaml.cs:
public sealed partial class SettingsPage : BasicPage{
public SettingsPage () {
InitializeComponent();
}
}
On the bold "BasicPage" there are the errors:
Base class of "...SettingsPage" differs from declared in other parts
and
Base type 'BasicPage' is already specified in other parts
Does someone know a solution?

I assume SettingsPage has a XAML part, which also needs to be derived from BasicPage:
<local:BasicPage x:Class="MyNamespace.SettingsPage" ...>
<!-- settings page content -->
</local:BasicPage>

Related

Using self created dll in Xamarin.Forms showing white screen on device

I am trying to create and use a DLL in Xamarin.Forms Project. This is given in the Charles Petzold's book 'Creating Mobile Apps using Xamarin.Form'.
It gives the following method to access the library that I have created
"From the PCL project of your application solution, add a reference to the library PCL assembly which is the dynamic-link library generated from the library project"
My library project is this
FILE: HslColorExtension.cs
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Xamarin.FormsBook.Toolkit
{
public static class Toolkit
{
public static void Init()
{
}
}
public class HslColorExtension : IMarkupExtension
{
public HslColorExtension()
{
}
public double H { set; get; }
public double S { set; get; }
public double L { set; get; }
public double A { set; get; }
public object ProvideValue(IServiceProvider servicePRovider)
{
return Color.FromHsla(H, S, L, A);
}
}
}
THE actual project is CustomExtensionDemo
In that the MainPage.xaml is
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="clr-namespace:Xamarin.FormsBook.Toolkit;assemby=Xamarin.FormsBook.Toolkit"
x:Class="CustomExtensionDemo.MainPage">
<StackLayout>
<Label Text="UTKARSH">
<Label.BackgroundColor>
<toolkit:HslColorExtension H="0" S="1" L="0.5"/>
</Label.BackgroundColor>
</Label>
</StackLayout>
</ContentPage>
THE METHOD HOW I ADDED THE DLL TO THE APPLICATION
FROM THE LIBRARY IS TOOK THE PATH THAT GENERATED THE DLL
C:\Users\admin\Desktop\Xamarin.FormsBook.Toolkit\Xamarin.FormsBook.Toolkit\obj\Debug
The name of the DLL is
Xamarin.FormsBook.Toolkit.dll
I added the reference to the actual project. browsed the path to
C:\Users\admin\Desktop\Xamarin.FormsBook.Toolkit\Xamarin.FormsBook.Toolkit\obj\Debug
and added the DLL : Xamarin.FormsBook.Toolkit.dll
Everything compiled correctly But I am getting a complete white screen on the Android Phone I am having .
POINTS:
1. I have set MainPage.xaml as the MainPage in the App.xaml.cs.. I have tried to put Label without the property element syntax and that worked.
I have not checked on iOS I think that there it would have the same problem as the problem could be in method of using the DLL in the application.
IDE:VS 2017
THE ERROR THAT IS DISCUSSED IN THE BELOW DISCUSSION
NOW I REQUIRE SOME WAY TO REMOVE THE "Windows Phone Silverlight 8.1" AND IT DOES NOT GIVE OPTION TO REMOVE THAT.
I'm fairly certain your problem is incompatible targets of your PCL.
Instead of adding the reference by browsing for the DLL, add it by selecting the project. This approach will check for compatibility of the DLL. Most likely you will need to change targets, fool around with nuget, etc.
Secondly I would recommend that your first platform for testing be UWP. There seems to be much better diagnostics on UWP. When I tried your code, I got the white screen on Android, but when using UWP as a platform I got an exception that said the HslColorExtension could not be found in the DLL.
If you follow these steps it should work for you:
You mis-spelled assembly as "assemby" in your XAML.
xmlns:toolkit="clr-namespace:Xamarin.FormsBook.Toolkit;assemby=Xamarin.FormsBook.Toolkit"
To make sure we're starting clean, let's just create a fresh Xamarin.FormsBook.Toolkit project and get rid of the one you've been using:
a) Copy out the code that you've already written so you don't lose it.
b) Create a new Xamarin.FormsBook.Toolkit project and only target those platforms that you would feasibly use this toolkit in. For example you would never use this in Silverlight because you're going to be referencing IMarkupExtension which is specific to Xamarin Forms.
c) Add a reference to your Toolkit project from your Xamarin Forms PCL project (looks like you're calling that "CustomExtensionDemo"). Don't reference the .dll but rather the project itself. This will spare you other headaches down the road.
d) Copy your HslColorExtension file (and any other classes you have) back in to the new project.
e) Add the Xamarin.Forms Nuget package to your Xamarin.FormsBook.Toolkit PCL, and a "using Xamarin.Forms" line at the top of your HslColorExtension file so that it recognizes the IMarkupExtension interface.
Add an empty Init function (or call it whatever you want) to your HslColorExtension class and then call it from your App.xaml.cs. This has the effect of "waking the compiler/linker up" to the fact that you have an assembly reference in XAML, since XAML is loaded at runtime. It does seem a bit hokey to have to do this, but you can tuck away that ugliness in your App.xaml.cs and you never have to see it again. If you're curious and want to see what's going on, try running it both with and without the call to Init, and take a look at your Android project's bin/Debug folder. When you call Init you'll see your Xamarin.FormsBook.Toolkit.dll appear in that folder. When you don't call Init it doesn't pull your assembly in. That's the core issue here.
Your resulting markup extension code would look like this:
public class HslColorExtension : IMarkupExtension
{
public static void Init()
{
}
public double H { set; get; }
public double S { set; get; }
public double L { set; get; }
public double A { set; get; }
public object ProvideValue(IServiceProvider serviceProvider)
{
return Color.FromHsla(H, S, L, A);
}
}
And your App.xaml.cs like this:
public partial class App : Application
{
public App()
{
HslColorExtension.Init();
InitializeComponent();
MainPage = new MainPage();
}
...
}
If that doesn't solve it for you, let me know! :-)

MVVMCross - Navigate ViewModel

When I try to open a new viewmodel I'm receiving the following error:
Failed to load ViewModel for type EasyBudget.Core.ViewModels.GridCategoryViewModel from locator MvxDefaultViewModelLocator
It´s also shows:
No symbols found.
And shows that cannot find or open the PDB file.
My viewmodel:
public class HomeViewModel
: MvxViewModel
{
private Cirrious.MvvmCross.ViewModels.MvxCommand _listCommandCategory;
public System.Windows.Input.ICommand ListCommandCategory
{
get
{
_listCommandCategory = _listCommandCategory ?? new Cirrious.MvvmCross.ViewModels.MvxCommand(DoListCategory);
return _listCommandCategory;
}
}
private void DoListCategory()
{
ShowViewModel<GridCategoryViewModel>();
}
}
And my other viewmodel:
public partial class GridCategoryView : MvxPhonePage
{
public GridCategoryView()
{
InitializeComponent();
}
}
Does anyone knows what may I be forgeting?
Best regards
Wilton Ruffato Wonrath
I believe the problem will most likely be somewhere in the construction of the ViewModel:
perhaps the constructor itself isn't public?
perhaps one or more of the parameters for the constructor couldn't be found?
perhaps some code inside the constructor has thrown an exception
Where you posted 'my other viewmodel' you actually posted code only for your other view. Can you post the code for the ViewModel that accompanies that view?
If you enable your debugger to break on all Exceptions, then this will possibly help you to find the problem that occurs during loading (inside https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs).
If you want to a pdb for debugger symbols, then these can be found inside the folders of http://github.com/slodge/MvvmCross-Binaries - inside the VS2012/Release folders. We are also currently trying to work out how to distribute these via SymbolSource.org (first got the request/suggestion this week)
Finally, if you want to see trace from a Windows build and are using the release packages from nuget then you can do this by overriding CreateDebugTrace() in your Setup.cs file - e.g. try:
protected override IMvxTrace CreateDebugTrace()
{
return new MvxDebugTrace();
}
This will also allow you to add some debug trace to your Core code if you want to using:
Mvx.Trace(format, args...)
Mvx.Warning(format, args...)
Mvx.Error(format, args...)
Perhaps, you forgot about adding ViewModel type to your generic MvxPhonePage.
Try this:
public partial class GridCategoryView : MvxPhonePage<GridCategoryViewModel>

Windows Phone inherited application page: designer error but compiles

I have a XAML Page which inherits from a userdefined class of mine:
<xpap:GenericListPage
x:Class="my.View.ListPage"
xmlns:xpap="clr-namespace:my.View.Generic" ... />
Initially I wanted to use a generic base class for my XAML page, but that was no possible (at least in WP7; is it possible in WP8 to use generic base classes for pages?). So I ended up with this work-around (ExtendedPhoneApplicationPage inherits from PhoneApplicationPage adds extra functionality/behaviour that all my pages like ListPage etc. should have in commen):
namespace my.View
{
namespace Generic
{
public partial class GenericListPage :
ExtendedPhoneApplicationPage<ListViewModel>
{
...
}
}
public partial class ListPage : Generic.GenericListPage
{
...
}
public class ExtendedPhoneApplicationPage<T> :
PhoneApplicationPage where T : class, IViewModelBase
{
...
}
}
This worked fine all together until I installed the WP8 SDK (as far as I can remember). The designer says "invalid markup" and there is an error saying: "The name "GenericListPage" does not exist in the namespace "clr-namespace:my.View.Generic"."
The strange part is, that the solution compiles and can be deployed on the emulator. Anyone know why or how to eliminate the error?

Localization and binding don't work together

I'm developing my first app and I'm trying to make it multilanguage.
Using AppHub example and some other link I created my resource files, fixed binding strings on my components and set a settings page.
First problem I had was that menu items and appbar buttons couldn't use localization strings (project complained when launched) so I have:
TextBlocks and other components binded with localized strings
Appbar buttons and items localized manually with a procedure loading localized strings
Now that I have my settings page, one item user can change is language.
Well, correct CultureInfo is selected according to user selection and then I use
Thread.CurrentThread.CurrentUICulture = Settings.Language;
When I press back button and return to main page, appbar items are localized correctly, while everything else is not.
The only workaround (that I really don't like, it's just to understand) is this:
public MainPage()
{
Thread.CurrentThread.CurrentUICulture = Settings.Language;
InitializeComponent();
// Everything else I need here
}
so I have to set language before components are created to make it work.
What's wrong? Which is the correct way to make a page refresh after changing language using binded strings?
I did not put a lot of code because I used basically the one provided in the link, but if you need more info I will edit my question.
I finally found a solution to automatically update my application components reacting to language change.
A good tutorial can be found here; briefly you must find a way to notify your app that localized resource is changed.
public class LocalizedStrings : ViewModelBase
{
private static AppResources localizedresources = new AppResources();
public AppResources LocalizedResources
{
get { return localizedresources; }
}
public void UpdateLanguage()
{
localizedresources = new AppResources();
RaisePropertyChanged(() => LocalizedResources);
}
public static LocalizedStrings LocalizedStringsResource
{
get
{
return Application.Current.Resources["LocalizedStrings"]
as LocalizedStrings;
}
}
}
With this when user change language, you should simply run
LocalizedStrings.LocalizedStringsResource.UpdateLanguage();
and the job is done.

Are there page checkers in WATIN which execute on each page load?

I am writing automation scripts using WATIR and WATIN. Watir has something called page checkers, which are code snippets that run on each page load. Is there something similar in WATIN ? I want a piece of code to run on each page load. Generally this is used to check for page errors or page loading images.
It is not really that easy to tell when page loads. I quickly googled about that page checkers in Watir, that you mentioned and found an article about page checkers in Watir. See first comment bellow the article. AFAIK it's really similar in WatiN.
Unfortunately, I don't see any similar functionality in WatiN (no event is fired after internal call to WaitForComplete. The easiest thing you could do is to subclass eg. IE class:
class MyIE : IE
{
public MyIE(string url) : base(url) { } //TODO: add constructors
public override void WaitForComplete(int waitForCompleteTimeOut)
{
base.WaitForComplete(waitForCompleteTimeOut);
Console.WriteLine("Page has been loaded");
}
}
However, the situation will be similar to described in mentioned comment (runs a lot more regularly than just page load).
I think that better approach would be using Page class from WatiN library. It is well documented. Example for watin.org webpage:
var ie = new MyIE("http://watin.org/");
var homePage = ie.Page<HomePage>();
Console.WriteLine(homePage.FirstFeature);
homePage.DocumentationLink.Click();
var documentationPage = ie.Page<DocumentationPage>();
Console.WriteLine(documentationPage.FAQLink.Url);
To run that code you need following classes:
abstract class WatiNBasePage : Page
{
[FindBy(Id = "header")]
public Div HeaderDiv { get; set; }
public Link HomeLink { get { return HeaderDiv.Link(Find.ByText("Home")); } }
public Link DocumentationLink { get { return HeaderDiv.Link(Find.ByText("Documentation")); } }
protected override void InitializeContents()
{
base.InitializeContents();
VerifyDocumentProperties(UnverifiedDocument, errorMessage => { throw new Exception(errorMessage); }); //TODO: modify if needed
}
protected override void VerifyDocumentProperties(Document document, Page.ErrorReporter errorReporter)
{
base.VerifyDocumentProperties(document, errorReporter);
if (!HomeLink.Exists)
errorReporter("HomeLink not exists");
//TODO: more checks here
}
}
class HomePage : WatiNBasePage
{
[FindBy(Id = "features")]
public Table FeatureTable { get; set; }
public string FirstFeature { get { return FeatureTable.Span(Find.First()).Text; } }
}
class DocumentationPage : WatiNBasePage
{
[FindBy(Text = "Frequently Asked Questions")]
public Link FAQLink { get; set; }
}
Basically you need to implement VerifyDocumentProperties. Above code will check if HomeLink exists, but maybe you would like to check if DocumentationLink exists etc. The second thing is to modify call to VerifyDocumentProperties. Now, if verification fails, Exception will be thrown after calling ie.Page<T>() (where T is a subclass of WatinBaseClass).
In my opinion, even if you don't need to use "page checkers", using Page class is still really useful and clarifies the code, so I really recommend using it. I regret that I haven't discovered it when I was starting work with WatiN.

Resources