Inheriting user control from an other user control - windows-phone-7

I am developing windows phone 8 app. Is would like to have some user controls which they inherit from a specific user control. I define my parent user control like this:
public abstract class WidgetsUserControl : UserControl
{
}
and the child like:
public partial class childControl : WidgetsUserControl
{
}
but it get error in defining abstract which says : "Missing partial modifier on declaration of type 'project1.WidgetsUserControl'; another partial declaration of this type exists"
what am i doing wrong?

It's complaining that you don't have the "partial" keyword in your definition of the WidgetsUserControl class. You probably have a corresponding XAML file for WidgetsUserControl that is partially defining that class, so here, you need to specify that it is partially defined as well.
public abstract partial class WidgetsUserControl : UserControl
Please note that if you go in this direction, then you also need to make the childControl definitions consistent. That is, in the code you would have:
public partial class childControl : WidgetsUserControl
and in the XAML, you will also need to specify that the childControl is a WidgetsUserControl, not just a simple UserControl:
<project1:WidgetsUserControl>
</project1:WidgetsUserControl>
Although this will work, a problem you will run into with this is that the Visual Studio Designer will complain that it can't create an instance of WidgetsUserControl (because it is abstract). This makes visualizing and editing the childControl a bit difficult. To solve this, my suggestion would be to just do away with the "abstract" in the base WidgetsUserControl. If you really want the base class to be abstract, then see here for other suggestions:
Abstract class in designer

This works good:
Code behind for parent user control:
public abstract partial class WidgetsUserControl : UserControl {
...
}
xaml for parent class:
<UserControl
...
xmlns:we="clr-namespace:project1"
...>
</UserControl>
code behind for child user control:
public partial class childControl : WidgetsUserControl {
...
}
xaml for child user control:
<we:WidgetsUserControl x:Class="project1.childControl"
...
xmlns:we="clr-namespace:project1"
...>
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"></Grid>
</we:WidgetsUserControl>

Related

Create a Base TagHelper with no TargetElement

I'm creating a library of MVC6 TagHelpers for a large project.
I find myself writing certain functionality in these TagHelpers again and again.
I'd like to make a base TagHelper that all the others inherit from to remove all the duplicated code.
The issue is this - suppose I create a base TagHelper as below:
public class BaseTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
//Some implementation...
}
}
Now, when I go to write a view, I will have intellisense suggesting the taghelper <base>.
Is there any way I can tell intellisense that this isn't a TagHelper I actually want to use, just a base class containing implementation common to other TagHelpers I've created?
Create it as an abstract class, see some examples in the official MVC Core repo like CacheTagHelperBase
public abstract class BaseTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
//Some base implementation...
}
}

How to connect View and ViewModel using MVVMCross for Windows Phone Universal Platform?

I have a pcl dll (Xamarin) and all the viewmodels, models, services, interfaces, converters are on this project.
I am already using this core on Android and IOS and now will start the Windows Phone app.
My main question for now is, how do I make a viewmodel the datacontext for a view. For sample: LoginViewModel.cs (core) and LoginView.xaml...
I am using MVVMCross and The Windows Phone project is 8.1
Than you in advance.
Updating:
I tried this:
<views:MvxWindowsPage
x:Class="Tocalivros.WindowsPhone.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Tocalivros.WindowsPhone.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:Cirrious.MvvmCross.WindowsCommon.Views"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
public sealed partial class LoginView : MvxWindowsPage<LoginViewModel>
{
public LoginView()
{
this.InitializeComponent();
}
But now I get this error: "Severity Code Description Project File Line
Error CS0263 Partial declarations of 'LoginView' must not specify different base classes"
Based on samples, I tried a different aproach, instead of making the view of type view model, I create a ViewModel as a property and set it as DataContext for the view.
Like this:
public sealed partial class LoginView : MvxWindowsPage
{
public new LoginViewModel ViewModel
{
get { return (LoginViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
public LoginView()
{
this.InitializeComponent();
this.DataContext = ViewModel;
}
But this way the viewmodel wont be initiated with the required parameters... (IServices)
It makes me think the better aproach is the first:
public sealed partial class LoginView : MvxWindowsPage<LoginViewModel> but I get error...
You can do like with every other platform in MvvmCross.
public class LoginPage : MvxWindowsPage<LoginViewModel>
{
}
Inside your view you will see something like this:
<views:MvxWindowsPage
x:Class="somenamspace.Views.LoginPage">
<!-- Put your layout here -->
</views:MvxWindowsPage>
The datacontext is now done for that view.

My databinding fails, is the path wrong?

I'm trying to create a simple RSS news aggregator. But I've done something wrong with the binding.
The xaml:
<phone:LongListSelector
x:Name="MainLongListSelector"
Margin="0,0,-12,0"
ItemsSource="{Binding Path=News.List}"
SelectionChanged="MainLongListSelector_SelectionChanged">
The code behind:
(mainpage)
public MainPage()
{
InitializeComponent();
// Set the data context of the LongListSelector control
// to the sample data
DataContext = App.ViewModel;
(viewmodel)
public class NewsViewModel : BaseViewModel
{
public NewsRepository News { get; private set; }
}
The NewsRepository is just an model object holding the List<obj> List. Could anyone point me in the direction where thing goes sideways?
The app runs just fine but the single item in the List I've put in manually does not show up in the application :-(
I've set and breakpoint at the assignment of the DataContext to the the viewmodels content its there...
Both NewsViewModel and NewsRepository have to implement INotifyPropertyChanged.
It would be easier if you use ObservableCollection<obj> as a source of your binding instead of custom NewsRepository object.

Partial declarations must not specify different base classes windows phone

i'am getting this error her:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}
my xaml is lile this:
<UserControl x:Class="GameMemory.MainPage"..../>
besides it returns erro: The call is ambiguous between the following methods or properties: 'GameMemory.MainPage.InitializeComponent()' and 'GameMemory.MainPage.InitializeComponent()'
any ideas please?
It seems that one of the assemblies you referenced has a class named UserControl.
Check your using declarations.

WP7 how to call Pivot from a class?

I have a Pivot on MainPage.xaml; how can I call this from another class?
MainPage.mypivot.Items.Add(p);
Error 2 An object reference is required for the non-static field, method,
or property '...MainPage.mypivot' H:\Users\Lacroix\documents\...
You can't do that exactly because of the reason the compiler tells you. MainPage is not a static class, when you view the page an instance of the class is being displayed, so you need to manipulate that instance. What you can do is pass a reference to the MainPage class to the other class. Then have MainPage contain an instance of the other class.
In MainPage.xaml.cs
public partial class MainPage : PhoneApplicationPage
{
private MyClass _myClass;
MainPage()
{
_myClass = new MyClass( this );
}
}
Now, MyClass will have access to the Pivot (and all other elements of MainPage).
If whatever logic the other class uses to add items to the Pivot is not available when displaying MainPage, i.e. you need to add items when displaying some other page, you'll need to create a static list (say PivotItemsList) in the App class. Have the other class add items to this list instead of directly to the Pivot. You can then access this within the MainPage constructor as App.PivotItemsList and add the items to the Pivot.

Resources