I am running Visual Studio 2013 Community Update 4 on x64 Windows 8.1 with all available updates applied as of 30-Dec-2014. I am running Windows 8.1 on VMWare Fusion 7 on a MacBook Pro w/ 2 processors and 4Gig allocated. Using C#, standard Blank template, and adding my own custom class. I have added the namespace to the MainPage.xaml:
<Page
x:Class="BindingWithValueConverters.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BindingWithValueConverters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:valueconverter="using:BindingWithValueConverters.Converters"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding WeatherViewModel, RelativeSource={RelativeSource Self}}">
<Page.Resources>
<valueconverter:DateToStringConverter x:Key="DateToStringConverter" />
</Page.Resources>
...
The "red squiggly" line remains under "valueconverter:DateToStringConverter"; If I delete the line, and start typing, "va..." Intellisense picks up "valueconverter:", so the namespace is "there". Then, the "DateToStringConverter" class shows as an option on autocomplete. Once accepted, after a brief delay, the red squiggly comes back specifying:
The type "DateToStringConverter" is not accessible.
The code builds and runs with no errors. I could continue to code, blindly. It would be optimal to fix this issue, though. Would appreciate guidance.
I have cleaned the project. I have deleted the *.suo file. The phone emulator runs as it does on a non-VM. I have run this exact project on a Windows 8.1 machine (no VM), and do not have this issue. I suspect Intellisense. Here is my converter class if helpful:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;
namespace BindingWithValueConverters.Converters
{
class DateToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
//throw new NotImplementedException();
DateTime date = (DateTime)value;
return String.Format("{0:dddd} - {0:d}", date);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
VMWare/Mac is the obvious difference between working and not; however, I am hopeful there is a parameter missing on my VMWare deployment or Visual Studio deployment regarding Intellisense that could resolve this. Intellisense is working everywhere else within this project.
Thanks!
Your converter class has not been given an explicit access modifier. Make it public and it'll show up just fine.
public class DateToStringConverter : IValueConverter
Related
I'm working through the book Head First C# and consistently have issues when adding resources to a window. This is a 100% repeatable error on any new WPF application I create when adding a new resource. The only way around this is to comment out the resource, build, and uncomment, as detailed in MVCE below. Images are included as proof this isn't a what-if or theoretical scenario.
What are the proper steps to add a resource file and use it within a WPF project?
I'm using Visual Studio Community 2017: Version 15.9.9
Target framework: .NET Framework 4.6.1
MVCE:
Create a new WPF application. Add a class:
//MyDataClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XAMLBuildErrorExample
{
class MyDataClass
{
public string Foo { get; set; }
}
}
Within MainWindow.xaml add a resource
<Window x:Class="XAMLBuildErrorExample.MainWindow"
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:local="clr-namespace:XAMLBuildErrorExample"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:MyDataClass x:Key="exampleResource" />
</Window.Resources>
<Grid>
</Grid>
</Window>
Attempt to build. Error "The tag 'MyDataClass' does not exist in XML namespace 'clr-namespace:XAMLBuildErrorExample'. Line 11 Position 10.":
Comment out the resource. Build succeeds:
Uncomment resource. Build succeeds whereas it failed before:
Any subsequent cleaning of the solution makes building impossible because of the error in the first image.
It appears the problem is tied to initial computer.
Tested on another work station VS Community 2017 version 15.9.11 and build was successful without any issues. Build>Clean>Build without issues.
I am very new to WPF programming, so please forgive if this is an obvious mistake on my part. I have a UserControl object that has the following Xaml:
<UserControl x:Class="SDMAS.LOOPAnalyzer.Views.Workspace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:SDMAS.LOOPAnalyzer.Models" >
<UserControl.Resources>
<!-- create an instance of our DataProvider class -->
<ObjectDataProvider x:Key="tblLOOPDataProvider" ObjectType="{x:Type local:tblLOOPDataProvider}"/>
<!-- define the method which is invoked to obtain our data -->
<ObjectDataProvider x:Key="LOOPEvents" ObjectInstance="{StaticResource tblLOOPDataProvider}" MethodName="GetData"/>
</UserControl.Resources>
<DockPanel DataContext="{Binding Source={StaticResource LOOPEvents}}">
<dg:DataGrid ItemsSource="{Binding}" Name="dataGrid"/>
</DockPanel>
</UserControl>
This code actually works. The records from the data table are correctly displayed in the application's view. The problem is the Visual Studio designer for this control thinks there is a problem, complaining about not being able to find the object type in the namespace: ObjectType="{x:Type local:tblLOOPDataProvider}"
The namespace does exist, and does contain the class, as proven by the fact the code is working. So what gives with the designer?
The following is screenshot of the error message:
Here is the tblLOOPDataProvider code
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SDMAS.LOOPAnalyzer.Models.LOOPDataSetTableAdapters;
namespace SDMAS.LOOPAnalyzer.Models
{
/// <summary>
/// Provides a singleton instance of the LOOPDataSet
/// </summary>
public class LOOPDataProvider
{
private static LOOPDataSet _loopDataSet;
public static LOOPDataSet LOOPDataSet
{
get
{
if (_loopDataSet == null)
{
_loopDataSet = new LOOPDataSet();
}
return _loopDataSet;
}
}
}
/// <summary>
/// A source of LOOP event objects
/// </summary>
public class tblLOOPDataProvider
{
private tblLOOPTableAdapter _adapter;
public tblLOOPDataProvider()
{
LOOPDataSet dataset = LOOPDataProvider.LOOPDataSet;
_adapter = new tblLOOPTableAdapter();
_adapter.Fill(LOOPDataProvider.LOOPDataSet.tblLOOP);
dataset.tblLOOP.tblLOOPRowChanged += new LOOPDataSet.tblLOOPRowChangeEventHandler(tblLOOPRowModified);
dataset.tblLOOP.tblLOOPRowDeleted += new LOOPDataSet.tblLOOPRowChangeEventHandler(tblLOOPRowModified);
}
void tblLOOPRowModified(object sender, LOOPDataSet.tblLOOPRowChangeEvent e)
{
_adapter.Update(LOOPDataProvider.LOOPDataSet.tblLOOP);
}
public DataView GetData()
{
return LOOPDataProvider.LOOPDataSet.tblLOOP.DefaultView;
}
}
}
Since I first posted this I have been hacking at various parts of the project, and now the designer won't display the Xaml at all. Just says Loading Designer... and appears to hang.
Some more background. I started this project based on the Prism4Demo on CodeProject (by David Veeneman). It has changed much since it's beginning, migrating to Prism 6 and updating other dependencies. That demo seemed a good template for an application that will have a number of modules to be developed independently by different staff members, likely with only one professional programmer to provide integration (not me). The class creating the designer problem is the "workspace" view for one of the modules that will eventually be wired to a view-model that in turn will be wired to a model consisting of a DataSet representing a LOOP event data table on a SQL server. In this current learning step, I have connected the server data table to a DataSet, which I am displaying in the workspace view directly without the view-model in the middle (and it is working, aside from the designer issue). I have based the code on internet examples that I don't yet understand all the way, but have been able to make work. So I don't yet understand the reason for the LOOPDataProvider class complexity, or the use of Resources in the Xaml.
So I continue to study the MVVM structure and my next step will be to wire up the DataSet-based model to the view-model, and the view-model to the view.
I've got a custom user control with a public property that I'd like to be able to set in XAML. Here it is below.
TestControl.xaml
<UserControl x:Class="Scale.Controls.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
TestControl.xaml.cs
using System.Windows.Controls;
namespace MyProject.Controls
{
public partial class TestControl : UserControl
{
public string TestMe { get; set; }
public TestControl()
{
InitializeComponent();
}
}
}
Then, in my MainWindow.xaml file, I try to include this:
<controls:TestControl TestMe="asdf" />
However, even though Visual Studio autocompletes the TestMe property, I then see things with a squiggly underline that says "The member "Test Me" is not recognized or is not accessible," as seen below.
I could have sworn I've done something like this before in other projects. How can I access (i.e. set) the public properties via XAML like this?
Visual Studio 2017
I had exactly the same problem. It was compiling one day ... and then it wasn't. I wasn't using DependencyProperty which shouldn't be needed as above. The properties were appearing in Intellisense but gave the same message when inserted. I cleaned, built, rebuilt, restarted VS, rebooted etc. All to no avail.
Last ditch try ... I removed all the offending attributes and got a clean compile. Then I put them back and it compiled. I really wasn't expecting that. Somehow VS had gotten its knickers in a twist.
If you are using VS2017, try to delete bin and obj folders in all projects of your solution, clean the solution and build again. It works for me !
You need to declare your property as Dependency Properties
namespace MyProject.Controls
{
public partial class TestControl : UserControl
{
//Register Dependency Property
public static readonly DependencyProperty TestMeDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(TestControl));
public string MyCar
{
get
{
return (string)GetValue(TestMeDependency);
}
set
{
SetValue(TestMeDependency, value);
}
}
public TestControl()
{
InitializeComponent();
}
}
}
Change the build target from AnyCPU to either x86 or x64. Unsure why AnyCPU does not work.
I know this is late but I just ran into this issue on VS 2020.
I tried all 3 options listed above and none of them worked including the CPU build.
I ended up having to right-click each project, clean, and rebuild. Then it solved the issue...
Really annoying this is still an issue.
In my case, there're no errors originally, after I modify my class, there're some errors in my class then the Xaml Error member is not recognized show. After solving errors in my class, I pass building the projects, all projects are built without errors, but the errors still show in the Error List Window. In the end, I restart Visual Studio, the errors disappear.
I faced this problem with my VS2022, I just removed WindowsBase reference, and it worked.
instead of setting multiple dependency properties for the control, I would use reflection.
public static readonly DependencyProperty UserControlProperty = DependencyProperty.Register("UserControl",
typeof(object), typeof(CustomUserControl), new PropertyMetadata(null));
public object UserControl
{
get { return GetValue(UserControlProperty); }
set { SetValue(UserControlProperty, value); }
}
I looked extensively online but I couldn't find anything really specific to help me out in this trouble I am having.
I am trying to make a custom renderer for a Button component, in Xamarin forms. Why am I not using the XLabs ImageButton component? Because it's not compatible with Android 4.0.3, which is my current Android target version.
I also tried writing on my own some code that is that much worthless that I am quite shy to post, unless you really require me to :)
I am in need of this code because, without it, images just fail to load in buttons while trying to use them in any button like this:
<Button Image="someImage.png"/>
I tried following a tutorial, here:
http://blog.falafel.com/learning-xamarin-custom-renderers-in-xamarin-forms/
But I failed adapting it to my needs.
Are no images being displayed at all? Check your locations of the images. Default location in ios is Resources folder, in android it is resources/drawable and in wp it is the root. If you want one image rather than 3 for each solution, put in to the PCL and set the build property to embedded resource and qualify the name with the namespace it is in. ie... ProjectName.Assets.image.png if it is in the PCL Assets directory. Not sure if this is your problem or not, but figured I would offer it up , just in case.
You can solve this by making an Image tap-able using TapGestureRecognizer. For implementation, go through this: http://www.c-sharpcorner.com/UploadFile/e04e9a/xamarin-forms-image-button-recipe/
The proper way to do this is described in Chapter 13 of Charles Petzold's excellent book on Xamarin Forms: https://developer.xamarin.com/guides/xamarin-forms/creating-mobile-apps-xamarin-forms/
My approach to this problem is (different than the book) to use a converter for the image file path. The Button.Image property is a FileImageSource object and it wants a file path. Unfortunately you cannot use embedded resources or a content file in the PCL. You must add an individual image file in each of the iOS, Android and UWP projects. The way I do this is to add the image to the PCL and use linking (option on the add existing file dialog).
So here is my converter approach to the above issue
<Button Image="{Binding Converter={StaticResource FileImageSourceConverter}, ConverterParameter=someImage.png}" />
The static resource...
<ContentPage.Resources>
<ResourceDictionary>
<local:FileImageSourceConverter x:Key="FileImageSourceConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
The converter...
public class FileImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string filename = parameter as string;
switch(Device.RuntimePlatform)
{
case Device.iOS:
case Device.Android:
default:
return filename;
case Device.Windows:
return Path.Combine("Images", filename);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The advantages of this approach is that (1) your XAML is not cluttered up with OnPlatform elements; (2) you don't need to put your images in the root directory of the UWP project; (3) much simpler than using a custom render to solve the same problem as some have suggested.
I have really strange problem. I've created WPF project in 2012 or 2013 VS it doesn't matter. I use .NET 4.5.
I add a new Activity (Workflow class) to that project. Its name is CustomActivity.
Then I add a new class that has got an attached property, example below:
public class AttachedObject : DependencyObject
{
public static readonly DependencyProperty NameProperty = DependencyProperty.RegisterAttached(
"Name",
typeof(string),
typeof(AttachedObject),
new FrameworkPropertyMetadata(
string.Empty,frameworkPropertyMetadataOptions.AffectsRender));
public static void SetName(ContentControl element, string value)
{
element.SetValue(NameProperty, value);
}
public static string GetName(ContentControl element)
{
return (string)element.GetValue(NameProperty);
}
}
The last step is to change MainWindow class that way:
public MainWindow()
{
InitializeComponent();
var activity = new CustomActivity();
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication1="clr-namespace:WpfApplication1;assembly=WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ContentControl wpfApplication1:AttachedObject.Name="MainArea"/>
</Grid>
</Window>
The problem is it doesn't compile because of below error:
Error 1 The type or namespace name 'CustomActivity' could not be found (are you missing a using directive or an assembly reference?) WpfApplication1\MainWindow.xaml.cs 13 32 WpfApplication1
CustomActivity has a default namespace. In obj folder there is CustomActivity.g.cs generated, so I have no idea what's going on.
It's 100% reproducible. When I remove using of CustomActivity or using of AttachedObject from xaml then the problem disappear.
Try replacing this:
xmlns:wpfApplication1="clr-namespace:WpfApplication1;assembly=WpfApplication1"
with this
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
The error you're seeing is due to a "known" issue in WPF applications that xaml namespaces that reference clr namespace from current assembly your in don't require the full assembly qualified name. If you were to declare a xaml namespace that references a clr namespace from another assembly, than you would have to specify the full name (with the ;[assemblyname] syntax).
Workflow Foundation has nothing to do with it.
EDIT:
Didn't realize it was a xaml activity.
But still, you can make it work, maybe, with a few hacks, but I wouldn't recommend it.
The reason you get that error is due to the different code generation and build action VS uses when creating xaml artifacts for WPF (Page):
System.Windows.Application.LoadComponent(this, resourceLocater);
and when creating xaml activities (XamlAppDef):
typeof(CustomActivity).Assembly.GetManifestResourceStream(resourceName);
If you turn your CustomActivity xaml build action to Page, the whole thing will compile - but i'm guessing something else might be broken someplace else...or not, who knows. My guess is that these two kinds of xaml were not meant to live together in a VS WPF application project template. But you can still define activities in a WF activity library, that way your activities will also be more easily reusable for other projects, WPF, console or even services.
I have the same issue under Visual Studio 2017.
The problem in my case is that Visual Studio is not compiling the Workflow activities before the code that use them.
To fix it, what I did is to move all workflows to other project dll, so visual Studio is forced to compile the workflows before the classes that make use of them.