I am trying to use a usercontrol defined outside the solution by referring the dll. It works if I use code; but not XAML
Here is the code -
1. UserControl code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace ComplexControl
{
[Preserve]
public class ComplexControl : ContentView
{
public ComplexControl()
{
var button = new Button
{
Text = "Click Me!",
//VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
int clicked = 0;
button.Clicked += (s, e) => button.Text = "Clicked: " + clicked++;
Content = button;
}
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
class PreserveAttribute : Attribute { }
}
}
For the usage in the project I Reference the assembly ConplexControl in PCL
It works with code ( ie. not using XAML )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using ComplexControl;
namespace CustomControlUsageExample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
// without InitializeComponent(); works
//this way works
//Step 1.
StackLayout sl = new StackLayout();
Label lb = new Label();
lb.Text = "Hello";
sl.Children.Add(lb);
sl.Children.Add(new ComplexControl.ComplexControl());
Content = sl;
}
}
}
If I use with XAML it does not work
Like this
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:local="clr-namespace:CustomControlUsageExample"
x:Class="CustomControlUsageExample.MainPage"
xmlns:custom="clr-namespace:ComplexControl;assembly:ComplexControl">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Label Text="Welcome to Xamarin Forms!"/>
<custom:ComplexControl.ComplexControl/>
</StackLayout>
</ContentPage>
And code behind for that is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using ComplexControl;
namespace CustomControlUsageExample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
//This way does not work
//Step 2
InitializeComponent();
}
}
}
I have already tried to debug thru and do not see it add the user control to content.
Can someone help me see what I am doing wrong?
It is works if I don't use XAML for the custom control.
I have a usercontrol work being done where the userControl is done using code; but it will be referred by projects inside XAML.
Thanks,
XP, you are doing everything correctly.
Unfortunately, small mistype (: instead of =) led you to this problem with XAML-defined control.
xmlns:custom="clr-namespace:ComplexControl;assembly:ComplexControl"
should be
xmlns:custom="clr-namespace:ComplexControl;assembly=ComplexControl"
Syntax for custom XAML namespace is clr-namespace:NAMESPACE;assembly=ASSEMBLY
I believe Xamarin.Forms should be smarter about namespaces and warn you about such mistypes in the editor or during build time (Xamarin.Forms does not compile XAML by default but nobody stops it from running simple syntax check, right?).
If you want, you can file a bug to Xamarin (it is easy!)
Related
The library was moved some years ago and the link I found for a wiki is stale.
I would like to add bit 127 to the Iso8583 class. I am using the below code but the program dies in the Pack() method, called from ToMsg(). I don't know what value to put in the length field. The field is a LLLVAR with a max length of 5, so is the length 5, or 8, or 999? All three values throw an exception in Pack().
What do I need to add to get bit 127 working?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OpenIso8583Net;
using OpenIso8583Net.FieldValidator;
using OpenIso8583Net.Formatter;
using OpenIso8583Net.LengthFormatters;
namespace MyLink
{
public class MyIso8583 : Iso8583
{
public new class Bit : Iso8583.Bit
{
public const int _127_DISCOVER_VERSION = 127;
}
// First you need to customise the template
// The message
private static readonly Template template;
static MyIso8583()
{
// Get the default template for the Iso8583 class
template = GetDefaultIso8583Template();
// change template to add bit 127 LLLVAR(5)
template.Add(Bit._127_DISCOVER_VERSION, FieldDescriptor.AsciiVar(3, 5, FieldValidators.AlphaNumericSpecial));
}
// override the base class using the template
public MyIso8583() : base(template)
{
}
protected override IField CreateField(int field)
{
return base.CreateField(field);
}
}
}
EDIT 3/24/20: I added an override to Bit and CreateField. I want the new bit 127 to act like a default LLLVAR of length 5.
This code works. It may not actually be necessary to add the CreateField override.
I've been following some tutorials to learn Xamarin/Xaml. I'm currently looking at custom panels.
My custom Panel class is currently as follows:
using System;
using System.Collections.Generic;
using System.Text;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
public class MyCustomPanel : Panel
{
protected override Size MeasureOverride(Size size)
{
return base.MeasureOverride(size);
}
}
With this code i get:
The type or namespace 'Size' could not be found (are you missing a
using directive or an assembly reference?)
Intellisense tells me (hovering over the base MeasureOverride) that the type of Size I'm looking for is Windows.Foundation.Size however I'm already using Windows.Foundation. It does not have a type of Size.
If I try to resolve with intellsence the only Size types availble to me are System.Drawing and Xamarin.Forms. None of these are suitable for MeasureOverride.
Can anyone help with this? What is it that I am missing?
Thanks in advance.
I'm developing with Silverlight 4 and Prism 4.
I'm also using Unity as my injection container.
I'm trying to create the module catalog from xaml, but I get this error "IModuleCatalog does not contain a definition of CreateFromXaml...".
My code snippet is:
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.MefExtensions;
namespace MyModularityProject {
public class MyBootStrapper : UnityBootstrapper {
protected override DependencyObject CreateShell() {
return ServiceLocator.Current.GetInstance<Shell>();
}
protected override void InitializeShell() {
base.InitializeShell();
Application.Current.RootVisual = (UIElement)Shell;
}
protected override IModuleCatalog CreateModuleCatalog() {
// This is the isntruction that doesn't compile
return ModuleCatalog.CreateFromXaml(new
Uri("/MyProject.Silverlight;component/ModulesCatalog.xaml",
UriKind.Relative));
}
}
}
What could I be missing here?
The reason that you need to add the full path to the ModuleCatalog type is that there is a ModuleCatalog property within the Bootstrapper base class that UnityBootstrapper inherits. If you don't qualify the name, you are essentially calling an accessor on a property which returns IModuleCatalog. The interface definition does not include this function.
I am using MEF, MVVM and Silverlight4 and below is my code
Main.cs:
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Diagnostics;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
public partial class Main : UserControl
{
public Main()
{
InitializeComponent();
// Satisfy the MEF imports for the class.
if (!DesignerProperties.IsInDesignTool)
{
CompositionInitializer.SatisfyImports(this);
}
}
/// <summary>
/// Sets the datacontext to the viewmodel for this view
/// </summary>
[Import(ViewModelTypes.MainViewModel)]
public object ViewModel
{
set
{
this.DataContext = value;
}
}
}
Viewmodel:
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows.Input;
[Export(ViewModelTypes.MainViewModel)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MainViewModel : ViewModelBase
{
[ImportingConstructor]
public MainViewModel(IAuthenticationModel authModel, IprospectManagementModel managementModel)
{
this.authenticationModel = authModel;
this.managementModel = managementModel;
}
/// <summary>
/// The authentication model.
/// </summary>
private IAuthenticationModel authenticationModel;
/// <summary>
/// The Iprospect management model.
/// </summary>
private IprospectManagementModel managementModel;
}
Below is the error i am getting, Please do help me out trace the same.
The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) No valid exports were found that match the constraint '(exportDefinition.ContractName == "MainViewModel")', invalid exports may have been rejected.
Resulting in:
Cannot set import 'IProspectCommonApp.Client.Main.ViewModel (ContractName="MainViewModel")' on part 'IProspectCommonApp.Client.Main'.
Element: IProspectCommonApp.Client.Main.ViewModel (ContractName="MainViewModel") --> IProspectCommonApp.Client.Main
It is probably failing because there is no IAuthenticationModel and/or IprospectManagementModel exported. The MainViewModel imports these via the ImportingConstructor, so it can't be created if they haven't been exported.
For more information on MEF debugging, see How to Debug and Diagnose MEF Failures.
Is there an equivalent method to WinForm's ProcessTabKey in Silverlight, or a way of correctly simulating it?
Looking around what I see are people are hard coding every single control (text1 has a KeyDown event to focus text2, text2 has a KeyDown event to focus text3, etc). A few have progressed to querying part of the layout hierarchy based on TabIndex, but by default all controls have the same TabIndex so it is once again a manual setup task.
A final wrinkle is dealing with nested containers. For example a vertically oriented stackpanel of textboxes, followed by a horizontally oriented stackpanel of buttons. Most programmatic approaches I've seen try to assume all controls are direct children of the same parent container.
I understand that Silverlight must operate under some restrictions, to prevent a Silverlight application from tabbing focus back to part of the browser (a potential security risk), but I hope there is some way to create a proper Enter-to-Tab setup without hand crafting all forms.
There is a related question in StackOverflow: Auto-tab in Silverlight 3
I'm using a custom attached Behavior (System.Windows.Interactivity) to avoid code behind because Iand works well with MVVM.
<UserControl x:Class="SCO.Ria.UI.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:Caliburn.Micro.Focus;assembly=Caliburn.Micro.Focus" mc:Ignorable="d"
d:DesignHeight="154" d:DesignWidth="468">
<interactivity:Interaction.Behaviors>
<local:TabNextBehavior/>
</interactivity:Interaction.Behaviors>
</UserControl>
TabNextBehavior.cs:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
namespace Caliburn.Micro.Focus {
public class TabNextBehavior : Behavior<Control> {
protected override void OnAttached() {
base.OnAttached();
this.AssociatedObject.KeyUp += AssociatedObject_KeyUp;
}
protected override void OnDetaching() {
base.OnDetaching();
this.AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
}
void AssociatedObject_KeyUp(object sender, KeyEventArgs args) {
if (args.Key == System.Windows.Input.Key.Enter) {
DependencyObject parent = null;
if (AssociatedObject is ChildWindow)
parent = ((ChildWindow)parent).Content as DependencyObject;
else parent = AssociatedObject;
parent.TabNext(); //extensin Method from VisualTreeExtensions.cs
}
}
}
You can see a gist here: https://gist.github.com/4576803