i've done this unity project following a youtube tutorial.
but i is not working can you figure out the problem??
ok so the first code is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AnswerScript : MonoBehaviour
{
public bool isCorrect = false;
public QuizManager quizManager;
public Color startc;
private void Start()
{
startc = GetComponent<Image>().color;
}
public void Answer()
{
if (isCorrect)
{
GetComponent<Image>().color = Color.green;
Debug.Log("Correct Answer");
quizManager.correct();
}
else
{
GetComponent<Image>().color = Color.red;
Debug.Log("Wrong Answer");
quizManager.wrong();
}
}
}
and the second is this one it is a little script
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class QuestionAndANWSER
{
public Image[] Question;
public string[] Answers;
public int CorrectAnswer;
}
the third is this one the biggest script:)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class QuizManager : MonoBehaviour
{
public List<QuestionAndANWSER> Qna;
public GameObject[] options;
public int currentQuestion;
public GameObject QuizPanel;
public GameObject GOPanel;
public Text QuestionTxt;
public Text ScoreTxt;
int totalQuestions = 0;
public int score;
private void Start()
{
totalQuestions = Qna.Count;
GOPanel.SetActive(false);
generateQuestion();
}
public void retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void GameOver()
{
QuizPanel.SetActive(false);
GOPanel.SetActive(true);
ScoreTxt.text = score +"/"+ totalQuestions;
}
public void correct()
{
score += 1;
Qna.RemoveAt(currentQuestion);
StartCoroutine(WaitforNext());
}
public void wrong()
{
Qna.RemoveAt(currentQuestion);
StartCoroutine(WaitforNext());
}
IEnumerator WaitforNext()
{
yield return new WaitForSeconds(1);
generateQuestion();
}
void SetAnswer()
{
for (int i= 0; i < options.Length; i++)
{
options[i].GetComponent<Image>().color = options[i].GetComponent<AnswerScript>().startc;
options[i].GetComponent<AnswerScript>().isCorrect = false;
options[i].transform.GetChild(0).GetComponent<Text>().text = Qna[currentQuestion].Answers[i];
if(Qna[currentQuestion].CorrectAnswer == i + 1)
{
options[i].GetComponent<AnswerScript>().isCorrect = true;
}
}
}
void generateQuestion()
{
if(Qna.Count > 0)
{
currentQuestion = Random.Range(0, Qna.Count);
QuestionTxt.text = Qna[currentQuestion].Question;
SetAnswer();
}
else
{
Debug.Log("out of questions");
GameOver();
}
}
}
it is a quiz template but i doesn't work. does anyone know how to fix that?
i am a begginer in learning c#
When you define a question and answer:
public class QuestionAndANWSER
{
public Image[] Question;
public string[] Answers;
public int CorrectAnswer;
}
you are storing questions as IMAGES. Later, in generateQuestion(), you're trying to use the questions as TEXT:
void generateQuestion()
{
// ...
QuestionTxt.text = Qna[currentQuestion].Question;
// ...
}
You have to be consistent in how you're handling the questions. You either need to change the QuizManager to use an image for the QuestionTxt or you need to change QuestionAndANSWER to use text for the Question.
Related
im using unity and i got this error:
and the code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CharacterManager : MonoBehaviour
{
public CharacterDatabase characterDB;
public Text nameText;
public Sprite artworkSprite;
private int selectedOption = 0;
// Start is called before the first frame update
void Start()
{
UpdateCharacter(selectedOption);
}
public void NextOption()
{
selectedOption++;
if(selectedOption >= characterDB.CharacterCount)
{
selectedOption = 0;
}
UpdateCharacter(selectedOption);
}
public void BackOption()
{
selectedOption--;
if(selectedOption < 0)
{
selectedOption = characterDB.CharacterCount - 1;
}
UpdateCharacter(selectedOption);
}
private void UpdateCharacter(int selectedOption)
{
Character character = characterDB.GetCharacter(selectedOption);
artworkSprite.sprite = character.characterSprite; <-- where the error is
nameText.text = character.characterName;
}
}
you can use 2 ways to set sprite
artworkSprite = character.characterSprite;
or
public Image artworkImage;
...
artworkImage.sprite = character.characterSprite;
I hope it will work on your project.
I have noticed the RecyclerView.Adapter type is not generic in Xamarin.Android. Why is it so? It seems to be defined as generic in native Android according to the documentation. Is there an underlying reason for this? Or is it some kind of backward compatiblity scenario?
You can create generic RecycleView in C# but not in java. In java you need to write code multiple times. That's why java android is worse even just simple event handler, Android Team Engineers are lazy to implements all data event handling because of the nature of java.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Android.Widget;
namespace XamarinAndroid.Basic.Core
{
public class GenericRecyclerViewAdapter<T> : RecyclerView.Adapter
{
/// <summary>
/// You can set this for different custom cardview
/// </summary>
public int CardViewResourceLayout { get; set; }
public ObservableCollection<T> Items { get; private set; }
public event EventHandler<RecyclerViewViewHolder> ItemViewTemplated;
public GenericRecyclerViewAdapter(IEnumerable<T> items, int cardViewResourceLayout) : base()
{
this.Items = new ObservableCollection<T>(items);
this.CardViewResourceLayout = cardViewResourceLayout;
this.Items.CollectionChanged += (sender, e) =>
{
try
{
this.NotifyDataSetChanged();
}
catch { }
};
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(CardViewResourceLayout, parent, false);
#if DEBUG
Log.Info("GenericRecyclerViewAdapter - ", CardViewResourceLayout.ToString());
#endif
RecyclerViewViewHolder vh = new RecyclerViewViewHolder(itemView);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
RecyclerViewViewHolder vh = holder as RecyclerViewViewHolder;
vh.ItemPosition = position;
vh.TemplateView.Tag = position;
vh.TemplateView.Click -= TemplateView_Click;
vh.TemplateView.Click += TemplateView_Click;
this.ItemViewTemplated?.Invoke(this, vh);
}
public event EventHandler<T> ItemClicked;
private void TemplateView_Click(object sender, EventArgs e)
{
var position = (int)((View)sender).Tag;
this.ItemClicked?.Invoke(this, this.Items[position]);
}
public override int ItemCount
{
get { return this.Items.Count; }
}
public override long GetItemId(int position)
{
return base.GetItemId(position);
}
}
public class RecyclerViewViewHolder : RecyclerView.ViewHolder
{
public View TemplateView { get; private set; }
public int ItemPosition { get; set; }
public RecyclerViewViewHolder(View itemView) : base(itemView)
{
// Locate and cache view references:
this.TemplateView = itemView;
}
}
}
Assigning data to each view
var recyclerViewToday = this.FindViewById<Android.Support.V7.Widget.RecyclerView>(Resource.Id.recyclerViewToday);
var layoutManager = new LinearLayoutManager(this);
recyclerViewToday.SetLayoutManager(layoutManager);
var adapter = new GenericRecyclerViewAdapter<FavoriteModel>(previousList, Resource.Layout.recent_list_item);
adapter.ItemViewTemplated += (d, holder) =>
{
var indicate = holder.ItemView.FindViewById<View>(Resource.Id.indicatorItemmRec);
indicate.SetBackgroundColor(ColorHelper.GetRandomLightColor());
(holder.ItemView.FindViewById<TextView>(Resource.Id.wordTxtRec)).Text = adapter.Items[holder.ItemPosition].Word;
var textView = (holder.ItemView.FindViewById<TextView>(Resource.Id.definitionTxtRec));
if (!string.IsNullOrEmpty(previousList[holder.ItemPosition].Definition))
textView.Text = previousList[holder.ItemPosition].Definition;
else
textView.Visibility = Android.Views.ViewStates.Gone;
};
recyclerViewToday.SetAdapter(adapter);
Result
Clean, Generic and Reusable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Text;
using Android.Text.Style;
using Android.Util;
using Android.Views;
using Android.Widget;
using Java.Util.Zip;
using ActionMenuView = Android.Support.V7.Widget.ActionMenuView;
namespace Android.Basic.Core
{
public class GenericRecyclerViewAdapter<T> : RecyclerView.Adapter
{
/// <summary>
/// You can set this for different custom cardview
/// </summary>
private int CardViewResourceLayout { get; set; }
public ObservableCollection<T> Items { get; private set; }
public event EventHandler<RecyclerViewViewHolder> ItemViewTemplated;
public RecyclerView.LayoutManager layoutManager;
public GenericRecyclerViewAdapter(RecyclerView recyclerView, IEnumerable<T> items, int cardViewResourceLayout, bool isList = true, bool isVertical = true) : base()
{
if(isList)
{
var vertical = isVertical ? LinearLayoutManager.Vertical : LinearLayoutManager.Horizontal;
layoutManager = new LinearLayoutManager(recyclerView.Context, vertical, false);
}
else
{
var vertical = isVertical ? GridLayoutManager.Vertical : GridLayoutManager.Horizontal;
layoutManager = new GridLayoutManager(recyclerView.Context, 3, vertical, false);
}
recyclerView.SetLayoutManager(layoutManager);
this.Items = new ObservableCollection<T>(items);
this.CardViewResourceLayout = cardViewResourceLayout;
this.Items.CollectionChanged += delegate
{
this.NotifyDataSetChanged();
};
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(CardViewResourceLayout, parent, false);
#if DEBUG
Log.Info("GenericRecyclerViewAdapter - ", CardViewResourceLayout.ToString());
#endif
RecyclerViewViewHolder vh = new RecyclerViewViewHolder(itemView);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
RecyclerViewViewHolder vh = holder as RecyclerViewViewHolder;
vh.ItemPosition = position;
vh.TemplateView.Tag = position;
vh.TemplateView.Click -= TemplateView_Click;
vh.TemplateView.Click += TemplateView_Click;
ItemViewTemplated?.Invoke(this, vh);
}
public event EventHandler<T> ItemClicked;
private void TemplateView_Click(object sender, EventArgs e)
{
var position = (int)((View)sender).Tag;
this.ItemClicked?.Invoke(sender, this.Items[position]);
}
public override int ItemCount
{
get { return this.Items.Count; }
}
public override long GetItemId(int position)
{
return base.GetItemId(position);
}
}
public class RecyclerViewViewHolder : RecyclerView.ViewHolder, View.IOnCreateContextMenuListener,
IMenuItemOnMenuItemClickListener
{
public View TemplateView { get; private set; }
public int ItemPosition { get; set; }
public event EventHandler<MenuInfo> ContextMenuCreated;
public event EventHandler<object> MenuItemClicked;
public MenuInfo MenuInfo { get; private set; }
public object Data { get; set; }
public RecyclerViewViewHolder(View itemView) : base(itemView)
{
// Locate and cache view references:
this.TemplateView = itemView;
this.TemplateView.SetOnCreateContextMenuListener(this);
}
public void OnCreateContextMenu(IContextMenu menu, View v, IContextMenuContextMenuInfo menuInfo)
{
MenuInfo = new MenuInfo(menu, v, menuInfo);
ContextMenuCreated?.Invoke(this, MenuInfo);
}
private Android.Views.MenuInflater menuInflater = null;
/// <summary>
/// After ContextMenuCreated
/// </summary>
/// <param name="resourcemenu"></param>
public void InflateMenu(int resourcemenu, SpannableString titleColor = null, object dta = null)
{
if (dta != null)
this.Data = dta;
if (this.TemplateView.Context is AppCompatActivity activity)
{
menuInflater = activity.MenuInflater;
}
else if (this.TemplateView.Context is Activity activity2)
{
menuInflater = activity2.MenuInflater;
}
var contextMenu = this.MenuInfo.ContextMenu;
contextMenu.Clear();
menuInflater.Inflate(resourcemenu, contextMenu);
var num = contextMenu.Size() - 1;
for (int i = 0; i <= num; i++)
{
var men = contextMenu.GetItem(i);
if(titleColor != null)
{
if (i == 0)
{
men.SetTitle(titleColor);
men.SetChecked(true);
}
}
if (i != 0)
{
men.SetOnMenuItemClickListener(this);
}
}
}
public bool OnMenuItemClick(IMenuItem item)
{
this.MenuItemClicked?.Invoke(item, this.Data);
return true;
}
public float PosX;
public float PosY;
}
public class MenuInfo
{
public IContextMenu ContextMenu { get; }
public View View { get; }
public IContextMenuContextMenuInfo ContextMenuInfo { get; }
public MenuInfo(IContextMenu contextMenu, View view, IContextMenuContextMenuInfo menuInfo)
{
this.ContextMenu = contextMenu;
this.View = view;
this.ContextMenuInfo = menuInfo;
}
}
}
Usage
RecyclerView recyclerView = context.FindViewById<RecyclerView>(Resource.Id.recycler);
var viewAdapter = new Android.Basic.Core.GenericRecyclerViewAdapter<StorageFile>(recyclerView, files, Resource.Layout.main_item);
viewAdapter.ItemViewTemplated += (dd, holder) =>
{
//CardView
CardView cardView = holder.ItemView.FindViewById<CardView>(Resource.Id.CategorycardView);
cardView.SetCardBackgroundColor(ThemeHelper.IsDark ? Color.ParseColor("#303030") : Color.White);
var indicator = holder.ItemView.FindViewById<LinearLayout>(Resource.Id.indicator);
indicator.SetBackgroundColor(ThemeHelper.IsDark ? ColorHelper.GetRandomLightColor() : ColorHelper.GetRandomDarkColor());
var file = files[holder.ItemPosition];
var itemIcon = holder.ItemView.FindViewById<ImageView>(Resource.Id.itemIcon);
var icon = file.GetFileThumbnail();
if (icon != null)
{
itemIcon.SetImageBitmap(icon);
}
TextView itemName = holder.ItemView.FindViewById<TextView>(Resource.Id.itemName);
itemName.Text = file.DisplayName;
itemName.SetTextColor(textColor);
};
viewAdapter.ItemClicked += (dd, ff) =>
{
var checkBttn = (dd as View).FindViewById<ImageButton>(Resource.Id.check_boxBttn);
};
recyclerView.SetAdapter(viewAdapter);
I am working with a new application written to version 8 (currently testing with 8.1.0.rc2).
The issue surrounds the "select all" checkbox that appears in the header of a Grid when using SelectionMode.MULTI. In particular, the problem is that the checkbox appears and operates as expected when the DataProvider implements InMemoryDataProvider, but the checkbox does not appear when the DataProvider implements BackEndDataProvider.
The following code creates two grids that differ only in whether they use InMemory or BackEnd:
public class Test {
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
private String name;
}
public class TestView extends BaseView {
public TestView() {
super("Test");
addComponent(new TestGrid(new TestDataProvider0()));
addComponent(new TestGrid(new TestDataProvider1()));
}
}
public class TestGrid extends Grid<Test> {
public TestGrid(DataProvider<Test, ?> dataProvider) {
setHeightByRows(4);
setSelectionMode(SelectionMode.MULTI);
setDataProvider(dataProvider);
addColumn(Test::getName).setCaption("Name");
}
}
public class TestDataProvider0 extends AbstractDataProvider<Test, SerializablePredicate<Test>> implements
BackEndDataProvider<Test, SerializablePredicate<Test>> {
public Stream<Test> fetch(Query<Test, SerializablePredicate<Test>> query) {
List<Test> tests = new ArrayList<>(query.getLimit());
for (int i = 0; i < query.getLimit(); i++) {
Test test = new Test();
test.setName(String.valueOf(query.getOffset() + i));
tests.add(test);
}
return tests.stream();
}
public int size(Query<Test, SerializablePredicate<Test>> query) {
return 100;
}
public void setSortOrders(List<QuerySortOrder> sortOrders) {
}
}
public class TestDataProvider1 extends AbstractDataProvider<Test, SerializablePredicate<Test>> implements
InMemoryDataProvider<Test> {
public Stream<Test> fetch(Query<Test, SerializablePredicate<Test>> query) {
List<Test> tests = new ArrayList<>(query.getLimit());
for (int i = 0; i < query.getLimit(); i++) {
Test test = new Test();
test.setName(String.valueOf(query.getOffset() + i));
tests.add(test);
}
return tests.stream();
}
public int size(Query<Test, SerializablePredicate<Test>> query) {
return 100;
}
public SerializablePredicate<Test> getFilter() {
return null;
}
public void setFilter(SerializablePredicate<Test> filter) {
}
public SerializableComparator<Test> getSortComparator() {
return null;
}
public void setSortComparator(SerializableComparator<Test> comparator) {
}
}
Here is how the grids are rendered:
Have I missed a critical step in setting up my BackEnd-based data provider/grid? The related documentation does not seem to address this issue.
Is there a known issue related to this?
Is select-all not available by design? Obviously, this could interact really badly with the concept of lazy-loading on a large data set...
MultiSelectionModelImpl has this method:
protected void updateCanSelectAll() {
switch (selectAllCheckBoxVisibility) {
case VISIBLE:
getState(false).selectAllCheckBoxVisible = true;
break;
case HIDDEN:
getState(false).selectAllCheckBoxVisible = false;
break;
case DEFAULT:
getState(false).selectAllCheckBoxVisible = getGrid()
.getDataProvider().isInMemory();
break;
default:
break;
}
}
This indicates that the default behavior for non-in-memory providers is to not show the select-all checkbox, but that this behavior can be overridden by setting the visibility to VISIBLE.
Tweaking the original code here:
public class TestGrid extends Grid<Test> {
public TestGrid(DataProvider<Test, ?> dataProvider) {
setHeightByRows(4);
MultiSelectionModel<Test> selectionModel = (MultiSelectionModel<Test>) setSelectionMode(SelectionMode.MULTI);
selectionModel.setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility.VISIBLE);
setDataProvider(dataProvider);
addColumn(Test::getName).setCaption("Name");
}
}
Specifically, this call is required for the checkbox to appear for data providers that implement BackEndDataProvider:
MultiSelectionModel<Test> selectionModel = (MultiSelectionModel<Test>) setSelectionMode(SelectionMode.MULTI);
selectionModel.setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility.VISIBLE);
With this change, the select-all checkbox now appears:
I am getting following exception when implementing the code below the exception.
Java.lang.IllegalStateException: The specific child already has a parent. you must call the removeView() on the child's parent first.
using System;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Android.App;
namespace Recycle.Droid
{
internal class albumadapter1 : RecyclerView.Adapter
{
// private int[] imageid;
// private Photo_album palbum;
// private _Recycler _Recycler;
private int[] imageid;
private Activity mainActivity;
public albumadapter1(Activity context, int[] imageid)
{
this.mainActivity = context;
this.imageid = imageid;
}
public override int ItemCount
{
get
{
return imageid.Length;
}
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
PhotoViewHolder vh1 = holder as PhotoViewHolder;
vh1.img.SetImageResource(imageid[position]);
//vh.Caption.Text = palbum[position].Caption;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemview = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.view_holder, parent);
PhotoViewHolder vh = new PhotoViewHolder(itemview);
return vh;
}
}
public class PhotoViewHolder : RecyclerView.ViewHolder
{
public TextView tv { get; private set; }
public ImageView img { get; private set; }
public PhotoViewHolder(View itemView) : base(itemView)
{
img = itemView.FindViewById<ImageView>(Resource.Id.imageView1);
// var Caption = itemView.FindViewById<TextView>(Resource.Id.textView);
// public PhotoViewHolder(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
}
}
}
please help me with problem.
Add False to the code below your issue will be solved
.......
...
...
..
.
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemview = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.view_holder, parent, false);
PhotoViewHolder vh = new PhotoViewHolder(itemview);
return vh;
}
.....
...
..
.
I set up a solr server running in Tomcat in machine 192.168.0.113(Centos 5.5).
And I deploy a website in matchine 192.168.0.114(Windows server 2003).
I use solrnet in matchine 192.168.0.114.
The full code like bellow(which have been edited thanks to #Paige Cook):
using System;
using System.Collections.Generic;
using System.Text;
using SolrNet;
using NUnit.Framework;
using SolrNet.Attributes;
using SolrNet.Commands.Parameters;
using Microsoft.Practices.ServiceLocation;
namespace MySolrNet
{
public class Video
{
private string videoid;
[SolrField("videoid")]
public string Videoid
{
get { return videoid; }
set { videoid = value; }
}
private string videoname;
[SolrField("videoname")]
public string Videoname
{
get { return videoname; }
set { videoname = value; }
}
private string videoorigin;
[SolrField("videoorigin")]
public string Videoorigin
{
get { return videoorigin; }
set { videoorigin = value; }
}
public Video(string id, string name, string origin)
{
this.Videoid = id;
this.Videoname = name;
this.Videoorigin = origin;
}
public Video()
{
}
public void FixtureSetup()
{
Startup.Init<Video>("http://192.168.0.113:8070/solr");
}
public void Add()
{
Video p = new Video("1", "test video", "Solr Test");
ISolrOperations<Video> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Video>>();
solr.Add(p);
solr.Commit();
}
}
[TestFixture]
public class VideoTests
{
[TestFixtureSetUp]
public void FixtureSetup()
{
Startup.Init<Video>("http://192.168.0.113:8070/solr");
}
[Test]
public void Add()
{
Video p = new Video("1", "test video", "Solr Test");
ISolrOperations<Video> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Video>>();
solr.Add(p);
solr.Commit();
}
[Test]
public void Query()
{
ISolrOperations<Video> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Video>>();
SolrQueryResults<Video> results = solr.Query(new SolrQueryByField("videoid", "33013"));
Assert.AreEqual(1, results.Count);
Console.WriteLine(results[0].Videoname);
}
}
}
However,both Add and Query test fail.
It complains: TestFixture failed: SetUp : System.IO.FileLoadException : Could not load file or assembly“SolrNet, Version=0.4.0.2002, Culture=neutral, PublicKeyToken=bc21753e8aa334cb” Or one of its dependencies.
But I have add reference to Microsoft.Practices.ServiceLocation.dll and SolrNet.dll in my projoect,is there any other dll files I just miss?
By the way,,I can access my solr sever in browser with this url:http://192.168.0.113:8070/solr.
Can anyone tell me:
Can I run solrnet and solr in different machines?
How to do it.
Thanks a lot!
I don't understand why you're using two different URLs when you Init Solr. Try changing the URL in Paige's application to the one you posted in your original question: http://192.168.0.113:8070/solr
Thanks for posting the code. The first thing I see is that you are using the test class as the class type to pass data to Solr. Split those out, that might be causing some issues. I would suggest the following:
public class Video
{
private string videoid;
[SolrField("videoid")]
public string Videoid
{
get { return videoid; }
set { videoid = value; }
}
private string videoname;
[SolrField("videoname")]
public string Videoname
{
get { return videoname; }
set { videoname = value; }
}
private string videoorigin;
[SolrField("videoorigin")]
public string Videoorigin
{
get { return videoorigin; }
set { videoorigin = value; }
}
}
[TestFixture]
public class VideoTests
{
[TestFixtureSetUp]
public void FixtureSetup()
{
Startup.Init<Video>("http://192.168.0.113/solr");
}
[Test]
public void Add() {
Video p = new Video("1","test video","Solr Test");
ISolrOperations<Video> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Video>>();
solr.Add(p);
solr.Commit();
}
[Test]
public void Query()
{
ISolrOperations<Video> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Video>>();
SolrQueryResults<Video> results = solr.Query(new SolrQueryByField("videoid", "33013"));
Assert.AreEqual(1, results.Count);
Console.WriteLine(results[0].Videoname);
}
}
Update:
Try this in a console application and see if it works...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SolrNet;
using Microsoft.Practices.ServiceLocation;
namespace Test1
{
class Program
{
static void Main(string[] args)
{
Startup.Init<Video>("http://192.168.0.113:8070/solr");
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Video>>();
var video = new Video("1", "test", "test");
solr.Add(video);
solr.Commit();
var results = solr.Query(SolrQuery.All);
Console.WriteLine("{0} - {1} - {2}",
results[0].Videoid, results[0].Videoname, results[0].Videoorigin);
}
}
public class Video
{
public Video(string id, string name, string origin)
{
Videoid = id;
Videoname = name;
Videoorigin = origin;
}
public string Videoid { get; set;
public string Videoname { get; set; }
public string Videoorigin { get; set; }
}
}
Can you check your project setup as suggested by Paige?
Do you have SolrNet source code added as project reference in your solution?
If you are using the dll, can you paste your .csproj file contents? If it is urgent, download the source code and add as project reference in your solution until you figure out the issue.